From 1f96d971f17cfb4dd4a1dcef0ffab05e968467cc Mon Sep 17 00:00:00 2001 From: chaffeqa Date: Tue, 16 Sep 2014 19:03:35 -0400 Subject: [PATCH 1/2] Correctly handle DOM Ready states Previously, if the script was loaded while in `document.readyState` of `interactive`, the `domReady` function is never called. See https://developer.mozilla.org/en-US/docs/Web/API/document.readyState for explanation of the proper detection of `DOMContentLoaded` --- js/utils/dom.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/js/utils/dom.js b/js/utils/dom.js index 22c4e24e4e..43fbbbb283 100644 --- a/js/utils/dom.js +++ b/js/utils/dom.js @@ -1,7 +1,7 @@ (function(window, document, ionic) { var readyCallbacks = []; - var isDomReady = false; + var isDomReady = document.readyState === 'complete' || document.readyState === 'interactive'; function domReady() { isDomReady = true; @@ -11,7 +11,10 @@ readyCallbacks = []; document.removeEventListener('DOMContentLoaded', domReady); } - document.addEventListener('DOMContentLoaded', domReady); + if (!isDomReady){ + document.addEventListener('DOMContentLoaded', domReady); + } + // From the man himself, Mr. Paul Irish. // The requestAnimationFrame polyfill @@ -110,7 +113,7 @@ * @param {function} callback The function to be called. */ ready: function(cb) { - if(isDomReady || document.readyState === "complete") { + if(isDomReady) { ionic.requestAnimationFrame(cb); } else { readyCallbacks.push(cb); From 6b29d44ce311ca8ff4157af857977fd15ccc4688 Mon Sep 17 00:00:00 2001 From: chaffeqa Date: Tue, 16 Sep 2014 19:11:19 -0400 Subject: [PATCH 2/2] Correctly handle window loaded states Previously if this script was executed after the window was loaded, none of the callbacks would fire. See https://developer.mozilla.org/en-US/docs/Web/API/document.readyState for definition of detecting the `load` event. TODO: Potentially this is redundent, since maybe using `ionic.DomUtil.ready` would mean one less listener. However I don't know if the listeners attached to `ionic.Platform.ready` require the `document.readyState` to be `complete` rather than only `interactive`. Linked to #2229 --- js/utils/platform.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/js/utils/platform.js b/js/utils/platform.js index df9b3a7a45..d81f3871b4 100644 --- a/js/utils/platform.js +++ b/js/utils/platform.js @@ -351,7 +351,8 @@ var platformName = null, // just the name, like iOS or Android platformVersion = null, // a float of the major and minor, like 7.1 - readyCallbacks = []; + readyCallbacks = [], + windowLoadListenderAttached; // setup listeners to know when the device is ready to go function onWindowLoad() { @@ -364,8 +365,17 @@ // cordova/phonegap object, so its just a browser, not a webview wrapped w/ cordova onPlatformReady(); } - window.removeEventListener("load", onWindowLoad, false); + if (windowLoadListenderAttached){ + window.removeEventListener("load", onWindowLoad, false); + } } + if (document.readyState === 'complete') { + onWindowLoad(); + } else { + windowLoadListenderAttached = true; + window.addEventListener("load", onWindowLoad, false); + } + window.addEventListener("load", onWindowLoad, false); function onPlatformReady() {