Permalink
Browse files

Initializing automatically when web_socket.js is dynamically loaded.

Using DOMContentLoaded to initialize earlier when available.
  • Loading branch information...
1 parent 1402307 commit 4f7b9f870eb69f7eb626260d8b972f573b447af4 @gimite committed Dec 11, 2011
Showing with 19 additions and 9 deletions.
  1. +19 −9 web_socket.js
View
@@ -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);
luciferous
luciferous Dec 12, 2011 Contributor

This section's readability could be improved by using swfobject.addDomLoadEvent and unwrapping WebSocket.__initialize:

  swfobject.addDomLoadEvent(WebSocket.__initialize);

swfobject.addDomLoadEvent checks document.readyState, so no need to do it here.

gimite
gimite Dec 14, 2011 Owner

Oh I didn't know swfobject provides such method. Thanks for letting me know. Changed to use it at:
841b01d

+ // This is supported by all browsers.
+ window.addEventListener("load", init, false);
} else {
- window.attachEvent("onload", function(){
- WebSocket.__initialize();
- });
+ window.attachEvent("onload", init);
}
}

0 comments on commit 4f7b9f8

Please sign in to comment.