DOMContentLoaded works in Opera 9b2+, so I have it firing for that now too - the...
[jquery.git] / event / event.js
index dcc0e88..a78e1a9 100644 (file)
@@ -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" ) {
+if ( $.browser == "mozilla" || $.browser == "opera" ) {
        // 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('<scr' + 'ipt id=__ie_init defer=true ' + 
+               'src=javascript:void(0)><\/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
-} else {
-       $.badReady = true;
+// If Safari  is used
+} else if ( $.browser == "safari" ) {
+       $.$$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) {