Permalink
Browse files

Thanks to @jdalton I decided to read through the IEContentLoaded arti…

…cle and realised I needed to fallback to checking the readyState property - for when the page is held within an <iframe>
  • Loading branch information...
1 parent 5cc40d0 commit 82421cca2dffbe61b98bf624246fcd2668a4b5b4 @Integralist committed Apr 14, 2011
Showing with 23 additions and 8 deletions.
  1. +23 −8 Assets/Scripts/DOMready.js
@@ -1,7 +1,9 @@
-var DOMready = (function(){
+var DOMready = (function() {
// Variables used throughout this script
- var queue = [],
+ var win = window,
+ doc = win.document,
+ queue = [],
exec,
loaded,
original_onload,
@@ -49,20 +51,33 @@ var DOMready = (function(){
document.addEventListener("DOMContentLoaded", process, false);
}
- // Internet Explorer versions less than 9 don't support DOMContentLoaded
- // But the doScroll('left') method appears to be the most reliable solution
+ // Internet Explorer versions less than 9 don't support DOMContentLoaded.
+ // The doScroll('left') method by Diego Perini (http://javascript.nwbox.com/IEContentLoaded/) appears to be the most reliable solution.
+ // Microsoft documentation explains the reasoning behind this http://msdn.microsoft.com/en-us/library/ms531426.aspx#Component_Initialization
@jdalton
jdalton Apr 17, 2011

no need for the isIE 9 check as it supports addEventListener

@Integralist
Integralist Apr 17, 2011 Owner

This is a check for less than IE9 so is needed. Unless Ive missed what you're saying?

if (isIE < 9) {
explorerTimer = window.setInterval(function() {
if (document.body) {
try {
document.createElement('div').doScroll('left');
window.clearInterval(explorerTimer);
return process();
- } catch(e) {
- console.log(e.message);
- }
+ } catch(e) {}
}
- }, 10);
+ }, 10);
+
+ // If our page is placed inside an <iframe> by another user then the above doScroll method wont work.
+ // As a secondary fallback for Internet Explorer we'll check the readyState property.
+ // Be aware that this will fire *just* before the window.onload event so isn't ideal.
+ doc.onreadystatechange = function() {
@jdalton
jdalton Apr 17, 2011

you should poll via setTimeout/Interval as well as some browsers don't fire an onreadystatechange event, but do update the document.readyState.

Also you should do the poll and the DOMContentLoaded/onreadystatechange all at once. The first one to detect dom-load wins and disables the others.

@Integralist
Integralist Apr 17, 2011 Owner

No probs I'll take a look at this end of next week.

+ if (doc.readyState == 'complete') {
+ // Clean-up
+ doc.onreadystatechange = null;
@jdalton
jdalton Apr 17, 2011

use addEventListener/attachEvent

@Integralist
Integralist Apr 26, 2011 Owner

Why's that? Is there a problem using window.onload as fallback?

@jdalton
jdalton Apr 26, 2011

It makes it more 3rd-party script friendly not to pave over others DOM0 event handlers they may have.

+ window.clearInterval(explorerTimer);
+
+ // Process function stack
+ process();
+ }
+ };
}
// Fall back to standard window.onload event

0 comments on commit 82421cc

Please sign in to comment.