From 4f7b9f870eb69f7eb626260d8b972f573b447af4 Mon Sep 17 00:00:00 2001 From: Hiroshi Ichikawa Date: Sun, 11 Dec 2011 22:42:26 +0900 Subject: Initializing automatically when web_socket.js is dynamically loaded. Using DOMContentLoaded to initialize earlier when available. --- web_socket.js | 28 +++++++++++++++++++--------- 1 file 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); } } -- cgit v1.2.1