diff options
author | Hiroshi Ichikawa <gimite@gmail.com> | 2011-12-11 22:42:26 +0900 |
---|---|---|
committer | Hiroshi Ichikawa <gimite@gmail.com> | 2011-12-11 22:42:26 +0900 |
commit | 4f7b9f870eb69f7eb626260d8b972f573b447af4 (patch) | |
tree | 9dd5bebcebe5c7dd69b8b250c1391e34f78f3a25 | |
parent | 14023075bfa7350a3a2268cb6d7e38e046b24bcf (diff) | |
download | web-socket-js-4f7b9f870eb69f7eb626260d8b972f573b447af4.tar.gz |
Initializing automatically when web_socket.js is dynamically loaded.
Using DOMContentLoaded to initialize earlier when available.
-rw-r--r-- | web_socket.js | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/web_socket.js b/web_socket.js index 6569c3c..aa7c5ea 100644 --- a/web_socket.js +++ b/web_socket.js @@ -213,6 +213,7 @@ WebSocket.CLOSING = 2; WebSocket.CLOSED = 3; + WebSocket.__initialized = false; WebSocket.__flash = null; WebSocket.__instances = {}; WebSocket.__tasks = []; @@ -232,7 +233,9 @@ * Loads WebSocketMain.swf and creates WebSocketMain object in Flash. */ WebSocket.__initialize = function() { - if (WebSocket.__flash) return; + + if (WebSocket.__initialized) return; + WebSocket.__initialized = true; if (WebSocket.__swfLocation) { // For backword compatibility. @@ -290,7 +293,9 @@ if (!e.success) { logger.error("[WebSocket] swfobject.embedSWF failed"); } - }); + } + ); + }; /** @@ -365,14 +370,19 @@ }; if (!window.WEB_SOCKET_DISABLE_AUTO_INITIALIZATION) { - if (window.addEventListener) { - window.addEventListener("load", function(){ - WebSocket.__initialize(); - }, false); + var init = function(){ + WebSocket.__initialize(); + }; + if (document.readyState == "complete") { + // Document is already loaded. + init(); + } else if (window.addEventListener) { + // This fires earlier but is not supported by all browsers. + document.addEventListener("DOMContentLoaded", init, false); + // This is supported by all browsers. + window.addEventListener("load", init, false); } else { - window.attachEvent("onload", function(){ - WebSocket.__initialize(); - }); + window.attachEvent("onload", init); } } |