X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=event%2Fevent.js;h=b705dbadc269f73b7631c0cb9f8af315ec215fa3;hb=598fcca346535040416d01aa6d2da53f8b1e8422;hp=dcc0e884a30c2ac0b861220c6252fd3f6180a158;hpb=08bb08fede9392f8c7047e88d6989e0b8583b31e;p=jquery.git diff --git a/event/event.js b/event/event.js index dcc0e88..b705dba 100644 --- a/event/event.js +++ b/event/event.js @@ -41,82 +41,76 @@ $.fn.hover = function(f,g) { }); }; -// Handle when the DOM is ready -$.ready = function(isFinal) { - // If the timer was running, stop it - if ( $.$$timer ) { - clearInterval( $.$$timer ); - $.$$timer = null; - } - - // If the last script to fire was in the body, - // we assume that it's trying to do a document.write - var s = document.getElementsByTagName("script"); - s = s[s.length-1].parentNode.nodeName == "HEAD"; +$.$$isReady = false; +$.$$ready = []; - // Only execute if we're doing a sane way, or the window - // is loaded, or the final script is in the head - // and there's something to execute - if ( ( !$.badReady || isFinal || s ) && $.$$ready ) { +// Handle when the DOM is ready +$.ready = function() { + $.$$isReady = true; + if ( $.$$ready ) { for ( var i = 0; i < $.$$ready.length; i++ ) { $.apply( document, $.$$ready[i] ); } - $.$$ready = null; + $.$$ready = []; } }; -// Based off of: -// http://linguiste.org/projects/behaviour-DOMContentLoaded/example.html - // If Mozilla is used if ( $.browser == "mozilla" ) { // Use the handy event callback document.addEventListener( "DOMContentLoaded", $.ready, null ); -// If IE is used +// If IE is used, use the excellent hack by Matthias Miller +// http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited } else if ( $.browser == "msie" ) { + + // Only works if you document.write() it + document.write('<\/script>'); + // Use the defer script hack - var script = document.createElement('SCRIPT'); - script.type = 'text/javascript'; - script.src = 'javascript:$.ready();void(0);'; - script.defer = true; - document.getElementsByTagName('HEAD')[0].appendChild(script); + var script = document.getElementById('__ie_init'); + script.onreadystatechange = function() { + if ( this.readyState == 'complete' ) { + $.ready(); + } + }; + + // Clear from memory script = null; -// Otherwise, try it the hacky way +// If Safari or Opera is used } else { - $.badReady = true; + $.$$timer = setInterval(function(){ + if ( document.readyState == "loaded" || + document.readyState == "complete" ) { + + clearInterval( $.$$timer ); + $.$$timer = null; + + $.ready(); + } + }, 10); } // A fallback, that will always work, just in case -$.event.add( window, "load", function(){ - $.ready(true); -}); +$.event.add( window, "load", $.ready ); /** * Bind a function to fire when the DOM is ready. */ $.fn.ready = function(f) { - return this.each(function(){ - if ( $.$$ready ) { - $.$$ready.push( f ); - } else { - var o = this; - $.$$ready = [ f ]; - - // Only do our hacky thing if we don't have the nice - // Mozilla or IE ways of doing it - if ( $.$$badReady ) { - // The trick is to check for the availability of a couple common - // DOM functions, if they exist, assume the DOM is ready - $.$$timer = setInterval( function(){ - if ( o && o.getElementsByTagName && o.getElementById && o.body ) { - $.ready(); - } - }, 10 ); - } + if ( $.$$isReady ) { + $.apply( document, f ); + } else { + if ( ! $.$$ready ) { + $.$$ready = []; } - }); + + $.$$ready.push( f ); + } + + return this; }; $.fn.toggle = function(a,b) {