Fixed the double-firing IE bug. (Ticket #13)
[jquery.git] / event / event.js
index 70111c6..fcd590a 100644 (file)
@@ -41,43 +41,50 @@ $.fn.hover = function(f,g) {
        });
 };
 
+$.$$isReady = false;
+$.$$ready = [];
+
 // Handle when the DOM is ready
-$.ready = function(isFinal) {
-       if ( $.$$ready ) {
-               for ( var i = 0; i < $.$$ready.length; i++ ) {
-                       $.apply( document, $.$$ready[i] );
+$.ready = function() {
+       if ( !$.$$isReady ) {
+               $.$$isReady = true;
+               if ( $.$$ready ) {
+                       for ( var i = 0; i < $.$$ready.length; i++ ) {
+                               $.apply( document, $.$$ready[i] );
+                       }
+                       $.$$ready = [];
                }
-               $.$$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:void 0';
-       script.defer = true;
+       var script = document.getElementById('__ie_init');
        script.onreadystatechange = function() {
-               if ( this.readyState == 'loading' ) {
+               if ( this.readyState == 'complete' ) {
                        $.ready();
                }
        };
-       document.getElementsByTagName('head')[0].appendChild(script);
+
+       // Clear from memory
        script = null;
 
-// If Safari or Opera is used
-} else {
+// If Safari  is used
+} else if ( $.browser == "safari" ) {
        $.$$timer = setInterval(function(){
-                if ( document.readyState == "loaded" || 
+       if ( document.readyState == "loaded" || 
                        document.readyState == "complete" ) {
 
                        clearInterval( $.$$timer );
@@ -89,21 +96,23 @@ if ( $.browser == "mozilla" ) {
 }
 
 // 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 ( $.$$isReady ) {
+               $.apply( document, f );
+       } else {
                if ( ! $.$$ready ) {
                        $.$$ready = [];
                }
 
                $.$$ready.push( f );
-       });
+       }
+
+       return this;
 };
 
 $.fn.toggle = function(a,b) {