Created
December 14, 2011 10:33
-
-
Save vseventer/1476064 to your computer and use it in GitHub Desktop.
Standalone DOMReady implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function(document) { | |
'use strict'; | |
/** | |
* Ready flag | |
* | |
* @access private | |
* @var boolean ready | |
*/ | |
var ready = false, | |
/** | |
* List of listeners | |
* | |
* @access private | |
* @var Array listeners | |
*/ | |
listeners = [], | |
/** | |
* Listener handler | |
* | |
* @access private | |
* @returns void | |
*/ | |
listener = function() { | |
ready = true;//flip switch | |
while(listeners[0]) { | |
listeners.shift()(); | |
} | |
}; | |
// Attach event | |
if(document.addEventListener) { | |
document.addEventListener('DOMContentLoaded', listener, false); | |
} | |
else {//mostly IE<9 | |
document.onreadystatechange = function() { | |
// Interactive equals DOMContentLoaded, but doesn't always fire | |
if(this.readyState.match(/interactive|complete/)) { | |
this.onreadystatechange = null;//unbind | |
listener(); | |
} | |
}; | |
} | |
/** | |
* Adds ready function | |
* | |
* @access public | |
* @param function fn | |
* @returns void | |
*/ | |
document.ready = function(fn) { | |
fn && (ready ? fn() : listeners.push(fn)); | |
}; | |
}(document)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(a){var c=false,b=[],d=function(){c=true;while(b[0]){b.shift()()}};if(a.addEventListener){a.addEventListener("DOMContentLoaded",d,false)}else{a.onreadystatechange=function(){if(this.readyState.match(/interactive|complete/)){this.onreadystatechange=null;d()}}}a.ready=function(e){e&&(c?e():b.push(e))}}(document)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment