Created
September 14, 2010 16:37
-
-
Save digitarald/579319 to your computer and use it in GitHub Desktop.
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
/* | |
--- | |
name: DomReady | |
description: Contains the custom event domready. | |
license: MIT-style license. | |
requires: [Browser, Element, Element.Event] | |
provides: DomReady | |
... | |
*/ | |
(function(window, document){ | |
var ready, | |
loaded, | |
checks = [], | |
shouldPoll, | |
timer, | |
isFramed = true; | |
// Thanks to Rich Dougherty <http://www.richdougherty.com/> | |
try { | |
isFramed = window.frameElement != null; | |
} catch(e){} | |
var domready = function(){ | |
clearTimeout(timer); | |
if (ready) return; | |
Browser.loaded = ready = true; | |
document.removeListener('DOMContentLoaded', domready).removeListener('readystatechange', check); | |
document.triggerEvent('domready'); | |
window.triggerEvent('domready'); | |
}; | |
var check = function(){ | |
for (var i = checks.length; i--; ) if (checks[i]()){ | |
domready(); | |
return true; | |
} | |
return false; | |
}; | |
var poll = function(){ | |
clearTimeout(timer); | |
if (!check()) timer = setTimeout(poll, 10); | |
}; | |
document.addListener('DOMContentLoaded', domready); | |
// doScroll technique by Diego Perini http://javascript.nwbox.com/IEContentLoaded/ | |
var testElement = document.createElement('div'); | |
if (testElement.doScroll && !isFramed){ | |
checks.push(function(){ | |
try { | |
testElement.doScroll(); | |
return true; | |
} catch (e){} | |
return false; | |
}); | |
shouldPoll = true; | |
} | |
if (document.readyState) checks.push(function(){ | |
var state = document.readyState; | |
return (state == 'loaded' || state == 'complete'); | |
}); | |
if ('onreadystatechange' in document) document.addListener('readystatechange', check); | |
else shouldPoll = true; | |
if (shouldPoll) poll(); | |
Element.Events.domready = { | |
onAdd: function(fn){ | |
if (ready) fn.call(this); | |
} | |
}; | |
// Make sure that domready fires before load | |
Element.Events.load = { | |
base: 'load', | |
onAdd: function(fn){ | |
if (loaded) fn.call(this); | |
}, | |
condition: function(){ | |
domready(); | |
return true; | |
} | |
}; | |
window.addEvent('load',function(){ | |
loaded = true; | |
}); | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment