1 | /*
|
---|
2 | DocumentReady Plug-in
|
---|
3 |
|
---|
4 | This plugin loads SWFUpload as soon as the document is ready. You should not load SWFUpload inside window.onload using this plugin.
|
---|
5 | You can also chain other functions by calling SWFUpload.DocumentReady(your function).
|
---|
6 |
|
---|
7 | Warning: Embedded Ads or other scripts that overwrite window.onload or use their own document ready functions may interfer with this plugin. You
|
---|
8 | should not set window.onload when using this plugin.
|
---|
9 |
|
---|
10 | Usage Example:
|
---|
11 |
|
---|
12 | var swfu = new SWFUpload(your settings object);
|
---|
13 | SWFUpload.DocumentReady(function () { alert('Document Ready!'; });
|
---|
14 |
|
---|
15 | */
|
---|
16 |
|
---|
17 | var SWFUpload;
|
---|
18 | if (typeof(SWFUpload) === "function") {
|
---|
19 | // Override iniSWFUpload so SWFUpload gets inited when the document is ready rather than immediately
|
---|
20 | SWFUpload.prototype.initSWFUpload = function (old_initSWFUpload) {
|
---|
21 | return function (init_settings) {
|
---|
22 | var self = this;
|
---|
23 | if (typeof(old_initSWFUpload) === "function") {
|
---|
24 | SWFUpload.DocumentReady(function () {
|
---|
25 | old_initSWFUpload.call(self, init_settings);
|
---|
26 | });
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | }(SWFUpload.prototype.initSWFUpload);
|
---|
31 |
|
---|
32 |
|
---|
33 | // The DocumentReady function adds the passed in function to
|
---|
34 | // the functions that will be executed when the document is ready/loaded
|
---|
35 | SWFUpload.DocumentReady = function (fn) {
|
---|
36 | // Add the function to the chain
|
---|
37 | SWFUpload.DocumentReady.InternalOnloadChain = function (previous_link_fn) {
|
---|
38 | return function () {
|
---|
39 | if (typeof(previous_link_fn) === "function") {
|
---|
40 | previous_link_fn();
|
---|
41 | }
|
---|
42 | fn();
|
---|
43 | };
|
---|
44 | }(SWFUpload.DocumentReady.InternalOnloadChain);
|
---|
45 | };
|
---|
46 | SWFUpload.DocumentReady.InternalOnloadChain = null;
|
---|
47 | SWFUpload.DocumentReady.Onload = function () {
|
---|
48 | // Execute the onload function chain
|
---|
49 | if (typeof(SWFUpload.DocumentReady.InternalOnloadChain) === "function") {
|
---|
50 | SWFUpload.DocumentReady.InternalOnloadChain();
|
---|
51 | }
|
---|
52 | };
|
---|
53 | SWFUpload.DocumentReady.SetupComplete = false;
|
---|
54 |
|
---|
55 |
|
---|
56 | /* ********************************************
|
---|
57 | This portion of the code gets executed as soon it is loaded.
|
---|
58 | It binds the proper event for executing JavaScript is
|
---|
59 | early as possible. This is a per browser function and so
|
---|
60 | some browser sniffing is used.
|
---|
61 |
|
---|
62 | This solution still has the "exposed" issue (See the Global Delegation section at http://peter.michaux.ca/article/553 )
|
---|
63 |
|
---|
64 | Base solution from http://dean.edwards.name/weblog/2006/06/again/ and http://dean.edwards.name/weblog/2005/09/busted/
|
---|
65 | ******************************************** */
|
---|
66 | if (!SWFUpload.DocumentReady.SetupComplete) {
|
---|
67 | // for Internet Explorer (using conditional comments)
|
---|
68 | /*@cc_on @*/
|
---|
69 | /*@if (@_win32)
|
---|
70 | document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
|
---|
71 | var script = document.getElementById("__ie_onload");
|
---|
72 | script.onreadystatechange = function() {
|
---|
73 | if (this.readyState == "complete") {
|
---|
74 | SWFUpload.DocumentReady.Onload(); // call the onload handler
|
---|
75 | }
|
---|
76 | };
|
---|
77 | SWFUpload.DocumentReady.SetupComplete = true;
|
---|
78 | /*@end @*/
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (!SWFUpload.DocumentReady.SetupComplete && /WebKit/i.test(navigator.userAgent)) { // sniff
|
---|
82 | var _timer = setInterval(function() {
|
---|
83 | if (/loaded|complete/.test(document.readyState)) {
|
---|
84 | clearInterval(_timer);
|
---|
85 | SWFUpload.DocumentReady.Onload(); // call the onload handler
|
---|
86 | }
|
---|
87 | }, 10);
|
---|
88 | SWFUpload.DocumentReady.SetupComplete = true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /* for Mozilla */
|
---|
92 | if (!SWFUpload.DocumentReady.SetupComplete && document.addEventListener) {
|
---|
93 | document.addEventListener("DOMContentLoaded", SWFUpload.DocumentReady.Onload, false);
|
---|
94 | SWFUpload.DocumentReady.SetupComplete = true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* for other browsers */
|
---|
98 | if (!SWFUpload.DocumentReady.SetupComplete) {
|
---|
99 | window.onload = SWFUpload.DocumentReady.Onload;
|
---|
100 | SWFUpload.DocumentReady.SetupComplete = true;
|
---|
101 | }
|
---|
102 | }
|
---|