Added the new jQuery.support object and removed all uses of jQuery.browser from withi...
[jquery.git] / src / event.js
index fa8be8d..9d06457 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * A number of helper functions used for managing events.
- * Many of the ideas behind this code orignated from 
+ * Many of the ideas behind this code originated from
  * Dean Edwards' addEvent library.
  */
 jQuery.event = {
@@ -13,80 +13,78 @@ jQuery.event = {
 
                // For whatever reason, IE has trouble passing the window object
                // around, causing it to be cloned in the process
-               if ( jQuery.browser.msie && elem.setInterval != undefined )
+               if ( elem.setInterval && elem != window )
                        elem = window;
 
                // Make sure that the function being executed has a unique ID
                if ( !handler.guid )
                        handler.guid = this.guid++;
-                       
-               // if data is passed, bind to handler 
-               if( data != undefined ) { 
-                       // Create temporary function pointer to original handler 
-                       var fn = handler; 
-
-                       // Create unique handler function, wrapped around original handler 
-                       handler = function() { 
-                               // Pass arguments and context to original handler 
-                               return fn.apply(this, arguments); 
-                       };
-
-                       // Store data in unique handler 
-                       handler.data = data;
 
-                       // Set the guid of unique handler to the same of original handler, so it can be removed 
-                       handler.guid = fn.guid;
+               // if data is passed, bind to handler
+               if ( data !== undefined ) {
+                       // Create temporary function pointer to original handler
+                       var fn = handler;
+
+                       // Create unique handler function, wrapped around original handler
+                       handler = this.proxy( fn, function() {
+                               // Pass arguments and context to original handler
+                               return fn.apply(this, arguments);
+                       });
+
+                       // Store data in unique handler
+                       handler.data = data;
                }
 
                // Init the element's event structure
                var events = jQuery.data(elem, "events") || jQuery.data(elem, "events", {}),
                        handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){
-                               // returned undefined or false
-                               var val;
-
                                // Handle the second event of a trigger and when
                                // an event is called after a page has unloaded
-                               if ( typeof jQuery == "undefined" || jQuery.event.triggered )
-                                       return val;
-               
-                               val = jQuery.event.handle.apply(elem, arguments);
-               
-                               return val;
+                               return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
+                                       jQuery.event.handle.apply(arguments.callee.elem, arguments) :
+                                       undefined;
                        });
-                       
-                       // Handle multiple events seperated by a space
-                       // jQuery(...).bind("mouseover mouseout", fn);
-                       jQuery.each(types.split(/\s+/), function(index, type) {
-                               // Namespaced event handlers
-                               var parts = type.split(".");
-                               type = parts[0];
-                               handler.type = parts[1];
-
-                               // Get the current list of functions bound to this event
-                               var handlers = events[type];
-
-                               // Init the event handler queue
-                               if (!handlers) {
-                                       handlers = events[type] = {};
-               
-                                       // Check for a special event handler
-                                       // Only use addEventListener/attachEvent if the special
-                                       // events handler returns false
-                                       if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem) === false ) {
-                                               // Bind the global event handler to the element
-                                               if (elem.addEventListener)
-                                                       elem.addEventListener(type, handle, false);
-                                               else if (elem.attachEvent)
-                                                       elem.attachEvent("on" + type, handle);
-                                       }
+               // Add elem as a property of the handle function
+               // This is to prevent a memory leak with non-native
+               // event in IE.
+               handle.elem = elem;
+
+               // Handle multiple events separated by a space
+               // jQuery(...).bind("mouseover mouseout", fn);
+               jQuery.each(types.split(/\s+/), function(index, type) {
+                       // Namespaced event handlers
+                       var parts = type.split(".");
+                       type = parts.shift();
+                       handler.type = parts.sort().join(".");
+
+                       // Get the current list of functions bound to this event
+                       var handlers = events[type];
+
+                       // Init the event handler queue
+                       if (!handlers) {
+                               handlers = events[type] = {};
+
+                               // Check for a special event handler
+                               // Only use addEventListener/attachEvent if the special
+                               // events handler returns false
+                               if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem,data) === false ) {
+                                       // Bind the global event handler to the element
+                                       if (elem.addEventListener)
+                                               elem.addEventListener(type, handle, false);
+                                       else if (elem.attachEvent)
+                                               elem.attachEvent("on" + type, handle);
                                }
+                       }
 
-                               // Add the function to the element's handler list
-                               handlers[handler.guid] = handler;
+                       // Add the function to the element's handler list
+                       handlers[handler.guid] = handler;
 
-                               // Keep track of which events have been used, for global triggering
-                               jQuery.event.global[type] = true;
-                       });
+                       // Keep track of which events have been used, for global triggering
+                       jQuery.event.global[type] = true;
+               });
+
+               // Nullify elem to prevent memory leaks in IE
+               elem = null;
        },
 
        guid: 1,
@@ -102,33 +100,34 @@ jQuery.event = {
 
                if ( events ) {
                        // Unbind all events for the element
-                       if ( types == undefined )
+                       if ( types === undefined || (typeof types === "string" && types.charAt(0) == ".") )
                                for ( var type in events )
-                                       this.remove( elem, type );
+                                       this.remove( elem, type + (types || "") );
                        else {
                                // types is actually an event object here
                                if ( types.type ) {
                                        handler = types.handler;
                                        types = types.type;
                                }
-                               
+
                                // Handle multiple events seperated by a space
                                // jQuery(...).unbind("mouseover mouseout", fn);
                                jQuery.each(types.split(/\s+/), function(index, type){
                                        // Namespaced event handlers
-                                       var parts = type.split(".");
-                                       type = parts[0];
-                                       
+                                       var namespace = type.split(".");
+                                       type = namespace.shift();
+                                       namespace = RegExp("(^|\\.)" + namespace.sort().join(".*\\.") + "(\\.|$)");
+
                                        if ( events[type] ) {
                                                // remove the given handler for the given type
                                                if ( handler )
                                                        delete events[type][handler.guid];
-                       
+
                                                // remove all handlers for the given type
                                                else
                                                        for ( handler in events[type] )
                                                                // Handle the removal of namespaced events
-                                                               if ( !parts[1] || events[type][handler].type == parts[1] )
+                                                               if ( namespace.test(events[type][handler].type) )
                                                                        delete events[type][handler];
 
                                                // remove generic event handler if no more handlers exist
@@ -150,6 +149,8 @@ jQuery.event = {
                        // Remove the expando if it's no longer used
                        for ( ret in events ) break;
                        if ( !ret ) {
+                               var handle = jQuery.data( elem, "handle" );
+                               if ( handle ) handle.elem = null;
                                jQuery.removeData( elem, "events" );
                                jQuery.removeData( elem, "handle" );
                        }
@@ -158,13 +159,21 @@ jQuery.event = {
 
        trigger: function(type, data, elem, donative, extra) {
                // Clone the incoming data, if any
-               data = jQuery.makeArray(data || []);
+               data = jQuery.makeArray(data);
+
+               if ( type.indexOf("!") >= 0 ) {
+                       type = type.slice(0, -1);
+                       var exclusive = true;
+               }
 
                // Handle a global trigger
                if ( !elem ) {
                        // Only trigger if we've ever bound an event for it
                        if ( this.global[type] )
-                               jQuery("*").add([window, document]).trigger(type, data);
+                               jQuery.each( jQuery.cache, function(){
+                                       if ( this.events && this.events[type] )
+                                               jQuery.event.trigger( type, data, this.handle.elem );
+                               });
 
                // Handle triggering a single element
                } else {
@@ -175,20 +184,32 @@ jQuery.event = {
                        var val, ret, fn = jQuery.isFunction( elem[ type ] || null ),
                                // Check to see if we need to provide a fake event, or not
                                event = !data[0] || !data[0].preventDefault;
-                       
+
                        // Pass along a fake event
-                       if ( event )
-                               data.unshift( this.fix({ type: type, target: elem }) );
+                       if ( event ) {
+                               data.unshift({
+                                       type: type,
+                                       target: elem,
+                                       preventDefault: function(){},
+                                       stopPropagation: function(){},
+                                       stopImmediatePropagation:stopImmediatePropagation,
+                                       timeStamp: now()
+                               });
+                               data[0][expando] = true; // no need to fix fake event
+                       }
 
                        // Enforce the right trigger type
                        data[0].type = type;
+                       if ( exclusive )
+                               data[0].exclusive = true;
 
-                       // Trigger the event
-                       if ( jQuery.isFunction( jQuery.data(elem, "handle") ) )
-                               val = jQuery.data(elem, "handle").apply( elem, data );
+                       // Trigger the event, it is assumed that "handle" is a function
+                       var handle = jQuery.data(elem, "handle");
+                       if ( handle )
+                               val = handle.apply( elem, data );
 
-                       // Handle triggering native .onfoo handlers
-                       if ( !fn && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
+                       // Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
+                       if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
                                val = false;
 
                        // Extra functions don't get the custom event object
@@ -198,9 +219,9 @@ jQuery.event = {
                        // Handle triggering of extra function
                        if ( extra && jQuery.isFunction( extra ) ) {
                                // call the extra function and tack the current return value on the end for possible inspection
-                               var ret = extra.apply( elem, data.concat( val ) );
+                               ret = extra.apply( elem, val == null ? data : data.concat( val ) );
                                // if anything is returned, give it precedence and have it overwrite the previous value
-                               if (ret !== undefined)
+                               if ( ret !== undefined )
                                        val = ret;
                        }
 
@@ -221,28 +242,32 @@ jQuery.event = {
 
        handle: function(event) {
                // returned undefined or false
-               var val;
+               var val, ret, namespace, all, handlers;
 
-               // Empty object is for triggered events with no data
-               event = jQuery.event.fix( event || window.event || {} ); 
+               event = arguments[0] = jQuery.event.fix( event || window.event );
 
                // Namespaced event handlers
-               var parts = event.type.split(".");
-               event.type = parts[0];
+               namespace = event.type.split(".");
+               event.type = namespace.shift();
 
-               var handlers = jQuery.data(this, "events") && jQuery.data(this, "events")[event.type], args = Array.prototype.slice.call( arguments, 1 );
-               args.unshift( event );
+               // Cache this now, all = true means, any handler
+               all = !namespace.length && !event.exclusive;
+               
+               namespace = RegExp("(^|\\.)" + namespace.sort().join(".*\\.") + "(\\.|$)");
+
+               handlers = ( jQuery.data(this, "events") || {} )[event.type];
 
                for ( var j in handlers ) {
                        var handler = handlers[j];
-                       // Pass in a reference to the handler function itself
-                       // So that we can later remove it
-                       args[0].handler = handler;
-                       args[0].data = handler.data;
 
                        // Filter the functions by class
-                       if ( !parts[1] || handler.type == parts[1] ) {
-                               var ret = handler.apply( this, args );
+                       if ( all || namespace.test(handler.type) ) {
+                               // Pass in a reference to the handler function itself
+                               // So that we can later remove it
+                               event.handler = handler;
+                               event.data = handler.data;
+
+                               ret = handler.apply( this, arguments );
 
                                if ( val !== false )
                                        val = ret;
@@ -251,31 +276,36 @@ jQuery.event = {
                                        event.preventDefault();
                                        event.stopPropagation();
                                }
+
+                               if( event._sip )
+                                       break;
+
                        }
                }
 
-               // Clean up added properties in IE to prevent memory leak
-               if (jQuery.browser.msie)
-                       event.target = event.preventDefault = event.stopPropagation =
-                               event.handler = event.data = null;
-
                return val;
        },
 
+       props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target timeStamp toElement type view wheelDelta which".split(" "),
+
        fix: function(event) {
-               // Short-circuit if the event has already been fixed by jQuery.event.fix
-               if ( event[ expando ] )
+               if ( event[expando] )
                        return event;
-                       
-               // store a copy of the original event object 
-               // and clone to set read-only properties
+
+               // store a copy of the original event object
+               // and "clone" to set read-only properties
                var originalEvent = event;
-               event = jQuery.extend({}, originalEvent);
-               
-               // Mark the event as fixed by jQuery.event.fix
-               event[ expando ] = true;
-               
-               // add preventDefault and stopPropagation since 
+               event = { originalEvent: originalEvent };
+
+               for ( var i = this.props.length, prop; i; ){
+                       prop = this.props[ --i ];
+                       event[ prop ] = originalEvent[ prop ];
+               }
+
+               // Mark it as fixed
+               event[expando] = true;
+
+               // add preventDefault and stopPropagation since
                // they will not work on the clone
                event.preventDefault = function() {
                        // if preventDefault exists run it on the original event
@@ -291,14 +321,19 @@ jQuery.event = {
                        // otherwise set the cancelBubble property of the original event to true (IE)
                        originalEvent.cancelBubble = true;
                };
-               
+
+               event.stopImmediatePropagation = stopImmediatePropagation;
+
+               // Fix timeStamp
+               event.timeStamp = event.timeStamp || now();
+
                // Fix target property, if necessary
                if ( !event.target )
                        event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either
-                               
+
                // check if target is a textnode (safari)
                if ( event.target.nodeType == 3 )
-                       event.target = originalEvent.target.parentNode;
+                       event.target = event.target.parentNode;
 
                // Add relatedTarget, if necessary
                if ( !event.relatedTarget && event.fromElement )
@@ -310,11 +345,11 @@ jQuery.event = {
                        event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0);
                        event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0);
                }
-                       
+
                // Add which for key events
-               if ( !event.which && (event.charCode || event.keyCode) )
+               if ( !event.which && ((event.charCode || event.charCode === 0) ? event.charCode : event.keyCode) )
                        event.which = event.charCode || event.keyCode;
-               
+
                // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
                if ( !event.metaKey && event.ctrlKey )
                        event.metaKey = event.ctrlKey;
@@ -323,80 +358,77 @@ jQuery.event = {
                // Note: button is not normalized, so don't use it
                if ( !event.which && event.button )
                        event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
-                       
+
                return event;
        },
-       
+
+       proxy: function( fn, proxy ){
+               // Set the guid of unique handler to the same of original handler, so it can be removed
+               proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
+               // So proxy can be declared as an argument
+               return proxy;
+       },
+
        special: {
                ready: {
-                       setup: function() {
-                               // Make sure the ready event is setup
-                               bindReady();
-                               return;
-                       },
-                       
-                       teardown: function() { return; }
-               },
-               
-               mouseenter: {
-                       setup: function() {
-                               if ( jQuery.browser.msie ) return false;
-                               jQuery(this).bind("mouseover", jQuery.event.special.mouseenter.handler);
-                               return true;
-                       },
-               
-                       teardown: function() {
-                               if ( jQuery.browser.msie ) return false;
-                               jQuery(this).unbind("mouseover", jQuery.event.special.mouseenter.handler);
-                               return true;
-                       },
-                       
-                       handler: function(event) {
-                               // If we actually just moused on to a sub-element, ignore it
-                               if ( withinElement(event, this) ) return true;
-                               // Execute the right handlers by setting the event type to mouseenter
-                               arguments[0].type = "mouseenter";
-                               return jQuery.event.handle.apply(this, arguments);
-                       }
-               },
-       
-               mouseleave: {
-                       setup: function() {
-                               if ( jQuery.browser.msie ) return false;
-                               jQuery(this).bind("mouseout", jQuery.event.special.mouseleave.handler);
-                               return true;
-                       },
-               
-                       teardown: function() {
-                               if ( jQuery.browser.msie ) return false;
-                               jQuery(this).unbind("mouseout", jQuery.event.special.mouseleave.handler);
-                               return true;
-                       },
-                       
-                       handler: function(event) {
-                               // If we actually just moused on to a sub-element, ignore it
-                               if ( withinElement(event, this) ) return true;
-                               // Execute the right handlers by setting the event type to mouseleave
-                               arguments[0].type = "mouseleave";
-                               return jQuery.event.handle.apply(this, arguments);
-                       }
+                       // Make sure the ready event is setup
+                       setup: bindReady,
+                       teardown: function() {}
                }
        }
 };
 
+function stopImmediatePropagation(){
+       this._sip = 1;
+       this.stopPropagation();
+}
+
+// Checks if an event happened on an element within another element
+// Used in jQuery.event.special.mouseenter and mouseleave handlers
+var withinElement = function(event) {
+       // Check if mouse(over|out) are still within the same parent element
+       var parent = event.relatedTarget;
+       // Traverse up the tree
+       while ( parent && parent != this )
+               try { parent = parent.parentNode; }
+               catch(e) { parent = this; }
+       
+       if( parent != this ){
+               // set the correct event type
+               event.type = event.data;
+               // handle event if we actually just moused on to a non sub-element
+               jQuery.event.handle.apply( this, arguments );
+       }
+};
+       
+jQuery.each({ 
+       mouseover: 'mouseenter', 
+       mouseout: 'mouseleave'
+}, function( orig, fix ){
+       jQuery.event.special[ fix ] = {
+               setup: function(){
+                       jQuery.event.add( this, orig, withinElement, fix );
+               },
+               teardown: function(){
+                       jQuery.event.remove( this, orig, withinElement );
+               }
+       };                         
+});
+
 jQuery.fn.extend({
        bind: function( type, data, fn ) {
                return type == "unload" ? this.one(type, data, fn) : this.each(function(){
                        jQuery.event.add( this, type, fn || data, fn && data );
                });
        },
-       
+
        one: function( type, data, fn ) {
+               var one = jQuery.event.proxy( fn || data, function(event) {
+                       jQuery(this).unbind(event, one);
+                       return (fn || data).apply( this, arguments );
+               });
                return this.each(function(){
-                       jQuery.event.add( this, type, function(event) {
-                               jQuery(this).unbind(event);
-                               return (fn || data).apply( this, arguments);
-                       }, fn && data);
+                       jQuery.event.add( this, type, one, fn && data);
                });
        },
 
@@ -413,31 +445,33 @@ jQuery.fn.extend({
        },
 
        triggerHandler: function( type, data, fn ) {
-               if ( this[0] )
-                       return jQuery.event.trigger( type, data, this[0], false, fn );
-               return undefined;
+               return this[0] && jQuery.event.trigger( type, data, this[0], false, fn );
        },
 
-       toggle: function() {
+       toggle: function( fn ) {
                // Save reference to arguments for access in closure
-               var args = arguments;
+               var args = arguments, i = 1;
 
-               return this.click(function(event) {
+               // link all the functions, so any of them can unbind this click handler
+               while( i < args.length )
+                       jQuery.event.proxy( fn, args[i++] );
+
+               return this.click( jQuery.event.proxy( fn, function(event) {
                        // Figure out which function to execute
-                       this.lastToggle = 0 == this.lastToggle ? 1 : 0;
-                       
+                       this.lastToggle = ( this.lastToggle || 0 ) % i;
+
                        // Make sure that clicks stop
                        event.preventDefault();
-                       
+
                        // and execute the function
-                       return args[this.lastToggle].apply( this, arguments ) || false;
-               });
+                       return args[ this.lastToggle++ ].apply( this, arguments ) || false;
+               }));
        },
 
        hover: function(fnOver, fnOut) {
-               return this.bind('mouseenter', fnOver).bind('mouseleave', fnOut);
+               return this.mouseenter(fnOver).mouseleave(fnOut);
        },
-       
+
        ready: function(fn) {
                // Attach the listeners
                bindReady();
@@ -446,12 +480,12 @@ jQuery.fn.extend({
                if ( jQuery.isReady )
                        // Execute the function immediately
                        fn.call( document, jQuery );
-                       
+
                // Otherwise, remember the function for later
                else
                        // Add the function to the wait list
                        jQuery.readyList.push( function() { return fn.call(this, jQuery); } );
-       
+
                return this;
        }
 });
@@ -465,20 +499,20 @@ jQuery.extend({
                if ( !jQuery.isReady ) {
                        // Remember that the DOM is ready
                        jQuery.isReady = true;
-                       
+
                        // If there are functions bound, to execute
                        if ( jQuery.readyList ) {
                                // Execute all of them
                                jQuery.each( jQuery.readyList, function(){
-                                       this.apply( document );
+                                       this.call( document );
                                });
-                               
+
                                // Reset the list of functions
                                jQuery.readyList = null;
                        }
-               
+
                        // Trigger any bound ready events
-                       $(document).triggerHandler("ready");
+                       jQuery(document).triggerHandler("ready");
                }
        }
 });
@@ -490,54 +524,63 @@ function bindReady(){
        readyBound = true;
 
        // Mozilla, Opera and webkit nightlies currently support this event
-       if ( document.addEventListener )
+       if ( document.addEventListener ) {
                // Use the handy event callback
-               document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
-       
-       // If Safari or IE is used
-       // Continually check to see if the document is ready
-       if (jQuery.browser.msie || jQuery.browser.safari ) (function(){
-               try {
-                       // If IE is used, use the trick by Diego Perini
-                       // http://javascript.nwbox.com/IEContentLoaded/
-                       if ( jQuery.browser.msie || document.readyState != "loaded" && document.readyState != "complete" )
+               document.addEventListener( "DOMContentLoaded", function(){
+                       document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
+                       jQuery.ready();
+               }, false );
+
+       // If IE event model is used
+       } else if ( document.attachEvent ) {
+               // ensure firing before onload,
+               // maybe late but safe also for iframes
+               document.attachEvent("onreadystatechange", function(){
+                       if ( document.readyState === "complete" ) {
+                               document.detachEvent( "onreadystatechange", arguments.callee );
+                               jQuery.ready();
+                       }
+               });
+
+               // If IE and not an iframe
+               // continually check to see if the document is ready
+               if ( document.documentElement.doScroll && !window.frameElement ) (function(){
+                       if ( jQuery.isReady ) return;
+
+                       try {
+                               // If IE is used, use the trick by Diego Perini
+                               // http://javascript.nwbox.com/IEContentLoaded/
                                document.documentElement.doScroll("left");
-               } catch( error ) {
-                       return setTimeout( arguments.callee, 0 );
-               }
+                       } catch( error ) {
+                               setTimeout( arguments.callee, 0 );
+                               return;
+                       }
 
-               // and execute any waiting functions
-               jQuery.ready();
-       })();
+                       // and execute any waiting functions
+                       jQuery.ready();
+               })();
+       }
 
        // A fallback to window.onload, that will always work
        jQuery.event.add( window, "load", jQuery.ready );
 }
 
 jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
-       "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," + 
-       "submit,keydown,keypress,keyup,error").split(","), function(i, name){
-       
+       "mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave," +
+       "change,select,submit,keydown,keypress,keyup,error").split(","), function(i, name){
+
        // Handle event binding
        jQuery.fn[name] = function(fn){
                return fn ? this.bind(name, fn) : this.trigger(name);
        };
 });
 
-// Checks if an event happened on an element within another element
-// Used in jQuery.event.special.mouseenter and mouseleave handlers
-var withinElement = function(event, elem) {
-       // Check if mouse(over|out) are still within the same parent element
-       var parent = event.relatedTarget;
-       // Traverse up the tree
-       while ( parent && parent != elem ) try { parent = parent.parentNode } catch(error) { parent = elem; };
-       // Return true if we actually just moused on to a sub-element
-       return parent == elem;
-};
-
 // Prevent memory leaks in IE
 // And prevent errors on refresh with events like mouseover in other browsers
 // Window isn't included so as not to unbind existing unload events
-jQuery(window).bind("unload", function() {
-       jQuery("*").add(document).unbind();
-});
+jQuery( window ).bind( 'unload', function(){ 
+       for ( var id in jQuery.cache )
+               // Skip the window
+               if ( id != 1 && jQuery.cache[ id ].handle )
+                       jQuery.event.remove( jQuery.cache[ id ].handle.elem );
+});