2 * A number of helper functions used for managing events.
3 * Many of the ideas behind this code orignated from
4 * Dean Edwards' addEvent library.
8 // Bind an event to an element
9 // Original by Dean Edwards
10 add: function(element, type, handler, data) {
11 // For whatever reason, IE has trouble passing the window object
12 // around, causing it to be cloned in the process
13 if ( jQuery.browser.msie && element.setInterval != undefined )
16 // if data is passed, bind to handler
20 // Make sure that the function being executed has a unique ID
22 handler.guid = this.guid++;
24 // Init the element's event structure
28 // Get the current list of functions bound to this event
29 var handlers = element.events[type];
31 // If it hasn't been initialized yet
33 // Init the event handler queue
34 handlers = element.events[type] = {};
36 // Remember an existing handler, if it's already there
37 if (element["on" + type])
38 handlers[0] = element["on" + type];
41 // Add the function to the element's handler list
42 handlers[handler.guid] = handler;
44 // And bind the global event handler to the element
45 element["on" + type] = this.handle;
47 // Remember the function in a global list (for triggering)
48 if (!this.global[type])
49 this.global[type] = [];
50 this.global[type].push( element );
56 // Detach an event or set of events from an element
57 remove: function(element, type, handler) {
59 if ( type && type.type )
60 delete element.events[ type.type ][ type.handler.guid ];
61 else if (type && element.events[type])
63 delete element.events[type][handler.guid];
65 for ( var i in element.events[type] )
66 delete element.events[type][i];
68 for ( var j in element.events )
69 this.remove( element, j );
72 trigger: function(type,data,element) {
73 // Clone the incoming data, if any
74 data = jQuery.makeArray(data || []);
76 // Handle a global trigger
78 var g = this.global[type];
80 for ( var i = 0, gl = g.length; i < gl; i++ )
81 this.trigger( type, data, g[i] );
83 // Handle triggering a single element
84 } else if ( element["on" + type] ) {
85 // Pass along a fake event
86 data.unshift( this.fix({ type: type, target: element }) );
89 element["on" + type].apply( element, data );
93 handle: function(event) {
94 if ( typeof jQuery == "undefined" ) return false;
96 event = jQuery.event.fix( event || window.event || {} ); // Empty object is for triggered events with no data
98 // returned undefined or false
101 var c = this.events[event.type];
103 var args = [].slice.call( arguments, 1 );
104 args.unshift( event );
107 // Pass in a reference to the handler function itself
108 // So that we can later remove it
109 args[0].handler = c[j];
110 args[0].data = c[j].data;
112 if ( c[j].apply( this, args ) === false ) {
113 event.preventDefault();
114 event.stopPropagation();
119 // Clean up added properties in IE to prevent memory leak
120 if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = event.handler = event.data = null;
125 fix: function(event) {
126 // Fix target property, if necessary
127 if ( !event.target && event.srcElement )
128 event.target = event.srcElement;
130 // Calculate pageX/Y if missing and clientX/Y available
131 if ( typeof event.pageX == "undefined" && typeof event.clientX != "undefined" ) {
132 var e = document.documentElement, b = document.body;
133 event.pageX = event.clientX + (e.scrollLeft || b.scrollLeft);
134 event.pageY = event.clientY + (e.scrollTop || b.scrollTop);
137 // check if target is a textnode (safari)
138 if (event.target.nodeType == 3) {
139 // store a copy of the original event object and clone because target is read only
140 var originalEvent = event;
141 event = jQuery.extend({}, originalEvent);
143 // get parentnode from textnode
144 event.target = originalEvent.target.parentNode;
146 // add preventDefault and stopPropagation since they will not work on the clone
147 event.preventDefault = function() {
148 return originalEvent.preventDefault();
150 event.stopPropagation = function() {
151 return originalEvent.stopPropagation();
155 // fix preventDefault and stopPropagation
156 if (!event.preventDefault) {
157 event.preventDefault = function() {
158 this.returnValue = false;
162 if (!event.stopPropagation) {
163 event.stopPropagation = function() {
164 this.cancelBubble = true;
175 * Binds a handler to a particular event (like click) for each matched element.
176 * The event handler is passed an event object that you can use to prevent
177 * default behaviour. To stop both default action and event bubbling, your handler
178 * has to return false.
180 * In most cases, you can define your event handlers as anonymous functions
181 * (see first example). In cases where that is not possible, you can pass additional
182 * data as the second paramter (and the handler function as the third), see
185 * @example $("p").bind( "click", function() {
186 * alert( $(this).text() );
188 * @before <p>Hello</p>
189 * @result alert("Hello")
191 * @example var handler = function(event) {
192 * alert(event.data.foo);
194 * $("p").bind( "click", {foo: "bar"}, handler)
195 * @result alert("bar")
196 * @desc Pass some additional data to the event handler.
198 * @example $("form").bind( "submit", function() { return false; } )
199 * @desc Cancel a default action and prevent it from bubbling by returning false
200 * from your function.
202 * @example $("form").bind( "submit", function(event) {
203 * event.preventDefault();
205 * @desc Cancel only the default action by using the preventDefault method.
208 * @example $("form").bind( "submit", function(event) {
209 * event.stopPropagation();
211 * @desc Stop only an event from bubbling by using the stopPropagation method.
215 * @param String type An event type
216 * @param Object data (optional) Additional data passed to the event handler as event.data
217 * @param Function fn A function to bind to the event on each of the set of matched elements
220 bind: function( type, data, fn ) {
221 return this.each(function(){
222 jQuery.event.add( this, type, fn || data, data );
227 * Binds a handler to a particular event (like click) for each matched element.
228 * The handler is executed only once for each element. Otherwise, the same rules
229 * as described in bind() apply.
230 The event handler is passed an event object that you can use to prevent
231 * default behaviour. To stop both default action and event bubbling, your handler
232 * has to return false.
234 * In most cases, you can define your event handlers as anonymous functions
235 * (see first example). In cases where that is not possible, you can pass additional
236 * data as the second paramter (and the handler function as the third), see
239 * @example $("p").one( "click", function() {
240 * alert( $(this).text() );
242 * @before <p>Hello</p>
243 * @result alert("Hello")
247 * @param String type An event type
248 * @param Object data (optional) Additional data passed to the event handler as event.data
249 * @param Function fn A function to bind to the event on each of the set of matched elements
252 one: function( type, data, fn ) {
253 return this.each(function(){
254 jQuery.event.add( this, type, function(event) {
255 jQuery(this).unbind(event);
256 return (fn || data).apply( this, arguments);
262 * The opposite of bind, removes a bound event from each of the matched
263 * elements. You must pass the identical function that was used in the original
266 * @example $("p").unbind( "click", function() { alert("Hello"); } )
267 * @before <p onclick="alert('Hello');">Hello</p>
268 * @result [ <p>Hello</p> ]
272 * @param String type An event type
273 * @param Function fn A function to unbind from the event on each of the set of matched elements
278 * Removes all bound events of a particular type from each of the matched
281 * @example $("p").unbind( "click" )
282 * @before <p onclick="alert('Hello');">Hello</p>
283 * @result [ <p>Hello</p> ]
287 * @param String type An event type
292 * Removes all bound events from each of the matched elements.
294 * @example $("p").unbind()
295 * @before <p onclick="alert('Hello');">Hello</p>
296 * @result [ <p>Hello</p> ]
302 unbind: function( type, fn ) {
303 return this.each(function(){
304 jQuery.event.remove( this, type, fn );
309 * Trigger a type of event on every matched element.
311 * @example $("p").trigger("click")
312 * @before <p click="alert('hello')">Hello</p>
313 * @result alert('hello')
317 * @param String type An event type to trigger.
320 trigger: function( type, data ) {
321 return this.each(function(){
322 jQuery.event.trigger( type, data, this );
326 // We're overriding the old toggle function, so
327 // remember it for later
328 _toggle: jQuery.fn.toggle,
331 * Toggle between two function calls every other click.
332 * Whenever a matched element is clicked, the first specified function
333 * is fired, when clicked again, the second is fired. All subsequent
334 * clicks continue to rotate through the two functions.
336 * Use unbind("click") to remove.
338 * @example $("p").toggle(function(){
339 * $(this).addClass("selected");
341 * $(this).removeClass("selected");
346 * @param Function even The function to execute on every even click.
347 * @param Function odd The function to execute on every odd click.
351 // save reference to arguments for access in closure
353 return typeof a[0] == "function" && typeof a[1] == "function" ? this.click(function(e) {
354 // Figure out which function to execute
355 this.lastToggle = this.lastToggle == 0 ? 1 : 0;
357 // Make sure that clicks stop
360 // and execute the function
361 return a[this.lastToggle].apply( this, [e] ) || false;
364 // Otherwise, execute the old toggle function
365 this._toggle.apply( this, arguments );
369 * A method for simulating hovering (moving the mouse on, and off,
370 * an object). This is a custom method which provides an 'in' to a
373 * Whenever the mouse cursor is moved over a matched
374 * element, the first specified function is fired. Whenever the mouse
375 * moves off of the element, the second specified function fires.
376 * Additionally, checks are in place to see if the mouse is still within
377 * the specified element itself (for example, an image inside of a div),
378 * and if it is, it will continue to 'hover', and not move out
379 * (a common error in using a mouseout event handler).
381 * @example $("p").hover(function(){
382 * $(this).addClass("over");
384 * $(this).addClass("out");
389 * @param Function over The function to fire whenever the mouse is moved over a matched element.
390 * @param Function out The function to fire whenever the mouse is moved off of a matched element.
393 hover: function(f,g) {
395 // A private function for handling mouse 'hovering'
396 function handleHover(e) {
397 // Check if mouse(over|out) are still within the same parent element
398 var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
400 // Traverse up the tree
401 while ( p && p != this ) try { p = p.parentNode } catch(e) { p = this; };
403 // If we actually just moused on to a sub-element, ignore it
404 if ( p == this ) return false;
406 // Execute the right function
407 return (e.type == "mouseover" ? f : g).apply(this, [e]);
410 // Bind the function to the two event listeners
411 return this.mouseover(handleHover).mouseout(handleHover);
415 * Bind a function to be executed whenever the DOM is ready to be
416 * traversed and manipulated. This is probably the most important
417 * function included in the event module, as it can greatly improve
418 * the response times of your web applications.
420 * In a nutshell, this is a solid replacement for using window.onload,
421 * and attaching a function to that. By using this method, your bound Function
422 * will be called the instant the DOM is ready to be read and manipulated,
423 * which is exactly what 99.99% of all Javascript code needs to run.
425 * Please ensure you have no code in your <body> onload event handler,
426 * otherwise $(document).ready() may not fire.
428 * You can have as many $(document).ready events on your page as you like.
429 * The functions are then executed in the order they were added.
431 * @example $(document).ready(function(){ Your code here... });
435 * @param Function fn The function to be executed when the DOM is ready.
439 // If the DOM is already ready
440 if ( jQuery.isReady )
441 // Execute the function immediately
444 // Otherwise, remember the function for later
446 // Add the function to the wait list
447 jQuery.readyList.push( f );
456 * All the code that makes DOM Ready work nicely.
461 // Handle when the DOM is ready
463 // Make sure that the DOM is not already loaded
464 if ( !jQuery.isReady ) {
465 // Remember that the DOM is ready
466 jQuery.isReady = true;
468 // If there are functions bound, to execute
469 if ( jQuery.readyList ) {
470 // Execute all of them
471 for ( var i = 0; i < jQuery.readyList.length; i++ )
472 jQuery.readyList[i].apply( document );
474 // Reset the list of functions
475 jQuery.readyList = null;
477 // Remove event lisenter to avoid memory leak
478 if ( jQuery.browser.mozilla || jQuery.browser.opera )
479 document.removeEventListener( "DOMContentLoaded", jQuery.ready, false );
487 * Bind a function to the scroll event of each matched element.
489 * @example $("p").scroll( function() { alert("Hello"); } );
490 * @before <p>Hello</p>
491 * @result <p onscroll="alert('Hello');">Hello</p>
495 * @param Function fn A function to bind to the scroll event on each of the matched elements.
496 * @cat Events/Browser
500 * Trigger the scroll event of each matched element. This causes all of the functions
501 * that have been bound to thet scroll event to be executed.
503 * @example $("p").scroll();
504 * @before <p onscroll="alert('Hello');">Hello</p>
505 * @result alert('Hello');
509 * @cat Events/Browser
513 * Bind a function to the submit event of each matched element.
515 * @example $("#myform").submit( function() {
516 * return $("input", this).val().length > 0;
518 * @before <form id="myform"><input /></form>
519 * @desc Prevents the form submission when the input has no value entered.
523 * @param Function fn A function to bind to the submit event on each of the matched elements.
528 * Trigger the submit event of each matched element. This causes all of the functions
529 * that have been bound to thet submit event to be executed.
531 * Note: This does not execute the submit method of the form element! If you need to
532 * submit the form via code, you have to use the DOM method, eg. $("form")[0].submit();
534 * @example $("form").submit();
535 * @desc Triggers all submit events registered for forms, but does not submit the form
543 * Bind a function to the focus event of each matched element.
545 * @example $("p").focus( function() { alert("Hello"); } );
546 * @before <p>Hello</p>
547 * @result <p onfocus="alert('Hello');">Hello</p>
551 * @param Function fn A function to bind to the focus event on each of the matched elements.
556 * Trigger the focus event of each matched element. This causes all of the functions
557 * that have been bound to thet focus event to be executed.
559 * Note: This does not execute the focus method of the underlying elements! If you need to
560 * focus an element via code, you have to use the DOM method, eg. $("#myinput")[0].focus();
562 * @example $("p").focus();
563 * @before <p onfocus="alert('Hello');">Hello</p>
564 * @result alert('Hello');
572 * Bind a function to the keydown event of each matched element.
574 * @example $("p").keydown( function() { alert("Hello"); } );
575 * @before <p>Hello</p>
576 * @result <p onkeydown="alert('Hello');">Hello</p>
580 * @param Function fn A function to bind to the keydown event on each of the matched elements.
581 * @cat Events/Keyboard
585 * Trigger the keydown event of each matched element. This causes all of the functions
586 * that have been bound to thet keydown event to be executed.
588 * @example $("p").keydown();
589 * @before <p onkeydown="alert('Hello');">Hello</p>
590 * @result alert('Hello');
594 * @cat Events/Keyboard
598 * Bind a function to the dblclick event of each matched element.
600 * @example $("p").dblclick( function() { alert("Hello"); } );
601 * @before <p>Hello</p>
602 * @result <p ondblclick="alert('Hello');">Hello</p>
606 * @param Function fn A function to bind to the dblclick event on each of the matched elements.
611 * Trigger the dblclick event of each matched element. This causes all of the functions
612 * that have been bound to thet dblclick event to be executed.
614 * @example $("p").dblclick();
615 * @before <p ondblclick="alert('Hello');">Hello</p>
616 * @result alert('Hello');
624 * Bind a function to the keypress event of each matched element.
626 * @example $("p").keypress( function() { alert("Hello"); } );
627 * @before <p>Hello</p>
628 * @result <p onkeypress="alert('Hello');">Hello</p>
632 * @param Function fn A function to bind to the keypress event on each of the matched elements.
633 * @cat Events/Keyboard
637 * Trigger the keypress event of each matched element. This causes all of the functions
638 * that have been bound to thet keypress event to be executed.
640 * @example $("p").keypress();
641 * @before <p onkeypress="alert('Hello');">Hello</p>
642 * @result alert('Hello');
646 * @cat Events/Keyboard
650 * Bind a function to the error event of each matched element.
652 * @example $("p").error( function() { alert("Hello"); } );
653 * @before <p>Hello</p>
654 * @result <p onerror="alert('Hello');">Hello</p>
658 * @param Function fn A function to bind to the error event on each of the matched elements.
659 * @cat Events/Browser
663 * Trigger the error event of each matched element. This causes all of the functions
664 * that have been bound to thet error event to be executed.
666 * @example $("p").error();
667 * @before <p onerror="alert('Hello');">Hello</p>
668 * @result alert('Hello');
672 * @cat Events/Browser
676 * Bind a function to the blur event of each matched element.
678 * @example $("p").blur( function() { alert("Hello"); } );
679 * @before <p>Hello</p>
680 * @result <p onblur="alert('Hello');">Hello</p>
684 * @param Function fn A function to bind to the blur event on each of the matched elements.
689 * Trigger the blur event of each matched element. This causes all of the functions
690 * that have been bound to thet blur event to be executed.
692 * Note: This does not execute the blur method of the underlying elements! If you need to
693 * blur an element via code, you have to use the DOM method, eg. $("#myinput")[0].blur();
695 * @example $("p").blur();
696 * @before <p onblur="alert('Hello');">Hello</p>
697 * @result alert('Hello');
705 * Bind a function to the load event of each matched element.
707 * @example $("p").load( function() { alert("Hello"); } );
708 * @before <p>Hello</p>
709 * @result <p onload="alert('Hello');">Hello</p>
713 * @param Function fn A function to bind to the load event on each of the matched elements.
714 * @cat Events/Browser
718 * Trigger the load event of each matched element. This causes all of the functions
719 * that have been bound to thet load event to be executed.
721 * Marked as private: Calling load() without arguments throws exception because the ajax load
722 * does not handle it.
724 * @example $("p").load();
725 * @before <p onload="alert('Hello');">Hello</p>
726 * @result alert('Hello');
731 * @cat Events/Browser
735 * Bind a function to the select event of each matched element.
737 * @example $("p").select( function() { alert("Hello"); } );
738 * @before <p>Hello</p>
739 * @result <p onselect="alert('Hello');">Hello</p>
743 * @param Function fn A function to bind to the select event on each of the matched elements.
748 * Trigger the select event of each matched element. This causes all of the functions
749 * that have been bound to thet select event to be executed.
751 * @example $("p").select();
752 * @before <p onselect="alert('Hello');">Hello</p>
753 * @result alert('Hello');
761 * Bind a function to the mouseup event of each matched element.
763 * @example $("p").mouseup( function() { alert("Hello"); } );
764 * @before <p>Hello</p>
765 * @result <p onmouseup="alert('Hello');">Hello</p>
769 * @param Function fn A function to bind to the mouseup event on each of the matched elements.
774 * Trigger the mouseup event of each matched element. This causes all of the functions
775 * that have been bound to thet mouseup event to be executed.
777 * @example $("p").mouseup();
778 * @before <p onmouseup="alert('Hello');">Hello</p>
779 * @result alert('Hello');
787 * Bind a function to the unload event of each matched element.
789 * @example $("p").unload( function() { alert("Hello"); } );
790 * @before <p>Hello</p>
791 * @result <p onunload="alert('Hello');">Hello</p>
795 * @param Function fn A function to bind to the unload event on each of the matched elements.
796 * @cat Events/Browser
800 * Trigger the unload event of each matched element. This causes all of the functions
801 * that have been bound to thet unload event to be executed.
803 * @example $("p").unload();
804 * @before <p onunload="alert('Hello');">Hello</p>
805 * @result alert('Hello');
809 * @cat Events/Browser
813 * Bind a function to the change event of each matched element.
815 * @example $("p").change( function() { alert("Hello"); } );
816 * @before <p>Hello</p>
817 * @result <p onchange="alert('Hello');">Hello</p>
821 * @param Function fn A function to bind to the change event on each of the matched elements.
826 * Trigger the change event of each matched element. This causes all of the functions
827 * that have been bound to thet change event to be executed.
829 * @example $("p").change();
830 * @before <p onchange="alert('Hello');">Hello</p>
831 * @result alert('Hello');
839 * Bind a function to the mouseout event of each matched element.
841 * @example $("p").mouseout( function() { alert("Hello"); } );
842 * @before <p>Hello</p>
843 * @result <p onmouseout="alert('Hello');">Hello</p>
847 * @param Function fn A function to bind to the mouseout event on each of the matched elements.
852 * Trigger the mouseout event of each matched element. This causes all of the functions
853 * that have been bound to thet mouseout event to be executed.
855 * @example $("p").mouseout();
856 * @before <p onmouseout="alert('Hello');">Hello</p>
857 * @result alert('Hello');
865 * Bind a function to the keyup event of each matched element.
867 * @example $("p").keyup( function() { alert("Hello"); } );
868 * @before <p>Hello</p>
869 * @result <p onkeyup="alert('Hello');">Hello</p>
873 * @param Function fn A function to bind to the keyup event on each of the matched elements.
874 * @cat Events/Keyboard
878 * Trigger the keyup event of each matched element. This causes all of the functions
879 * that have been bound to thet keyup event to be executed.
881 * @example $("p").keyup();
882 * @before <p onkeyup="alert('Hello');">Hello</p>
883 * @result alert('Hello');
887 * @cat Events/Keyboard
891 * Bind a function to the click event of each matched element.
893 * @example $("p").click( function() { alert("Hello"); } );
894 * @before <p>Hello</p>
895 * @result <p onclick="alert('Hello');">Hello</p>
899 * @param Function fn A function to bind to the click event on each of the matched elements.
904 * Trigger the click event of each matched element. This causes all of the functions
905 * that have been bound to thet click event to be executed.
907 * @example $("p").click();
908 * @before <p onclick="alert('Hello');">Hello</p>
909 * @result alert('Hello');
917 * Bind a function to the resize event of each matched element.
919 * @example $("p").resize( function() { alert("Hello"); } );
920 * @before <p>Hello</p>
921 * @result <p onresize="alert('Hello');">Hello</p>
925 * @param Function fn A function to bind to the resize event on each of the matched elements.
926 * @cat Events/Browser
930 * Trigger the resize event of each matched element. This causes all of the functions
931 * that have been bound to thet resize event to be executed.
933 * @example $("p").resize();
934 * @before <p onresize="alert('Hello');">Hello</p>
935 * @result alert('Hello');
939 * @cat Events/Browser
943 * Bind a function to the mousemove event of each matched element.
945 * @example $("p").mousemove( function() { alert("Hello"); } );
946 * @before <p>Hello</p>
947 * @result <p onmousemove="alert('Hello');">Hello</p>
951 * @param Function fn A function to bind to the mousemove event on each of the matched elements.
956 * Trigger the mousemove event of each matched element. This causes all of the functions
957 * that have been bound to thet mousemove event to be executed.
959 * @example $("p").mousemove();
960 * @before <p onmousemove="alert('Hello');">Hello</p>
961 * @result alert('Hello');
969 * Bind a function to the mousedown event of each matched element.
971 * @example $("p").mousedown( function() { alert("Hello"); } );
972 * @before <p>Hello</p>
973 * @result <p onmousedown="alert('Hello');">Hello</p>
977 * @param Function fn A function to bind to the mousedown event on each of the matched elements.
982 * Trigger the mousedown event of each matched element. This causes all of the functions
983 * that have been bound to thet mousedown event to be executed.
985 * @example $("p").mousedown();
986 * @before <p onmousedown="alert('Hello');">Hello</p>
987 * @result alert('Hello');
995 * Bind a function to the mouseover event of each matched element.
997 * @example $("p").mouseover( function() { alert("Hello"); } );
998 * @before <p>Hello</p>
999 * @result <p onmouseover="alert('Hello');">Hello</p>
1003 * @param Function fn A function to bind to the mousedown event on each of the matched elements.
1008 * Trigger the mouseover event of each matched element. This causes all of the functions
1009 * that have been bound to thet mousedown event to be executed.
1011 * @example $("p").mouseover();
1012 * @before <p onmouseover="alert('Hello');">Hello</p>
1013 * @result alert('Hello');
1020 var e = ("blur,focus,load,resize,scroll,unload,click,dblclick," +
1021 "mousedown,mouseup,mousemove,mouseover,mouseout,change,select," +
1022 "submit,keydown,keypress,keyup,error").split(",");
1024 // Go through all the event names, but make sure that
1025 // it is enclosed properly
1026 for ( var i = 0; i < e.length; i++ ) new function(){
1030 // Handle event binding
1031 jQuery.fn[o] = function(f){
1032 return f ? this.bind(o, f) : this.trigger(o);
1035 // Handle event unbinding
1037 jQuery.fn["un"+o] = function(f){ return this.unbind(o, f); };
1039 // Finally, handle events that only fire once
1041 jQuery.fn["one"+o] = function(f){
1042 // save cloned reference to this
1043 var element = jQuery(this);
1044 var handler = function() {
1045 // unbind itself when executed
1046 element.unbind(o, handler);
1048 // apply original handler with the same arguments
1049 return f.apply(this, arguments);
1051 return this.bind(o, handler);
1056 // If Mozilla is used
1057 if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
1058 // Use the handy event callback
1059 document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
1061 // If IE is used, use the excellent hack by Matthias Miller
1062 // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
1063 } else if ( jQuery.browser.msie ) {
1065 // Only works if you document.write() it
1066 document.write("<scr" + "ipt id=__ie_init defer=true " +
1067 "src=//:><\/script>");
1069 // Use the defer script hack
1070 var script = document.getElementById("__ie_init");
1071 if (script) // script does not exist if jQuery is loaded dynamically
1072 script.onreadystatechange = function() {
1073 if ( this.readyState != "complete" ) return;
1074 this.parentNode.removeChild( this );
1078 // Clear from memory
1081 // If Safari is used
1082 } else if ( jQuery.browser.safari ) {
1083 // Continually check to see if the document.readyState is valid
1084 jQuery.safariTimer = setInterval(function(){
1085 // loaded and complete are both valid states
1086 if ( document.readyState == "loaded" ||
1087 document.readyState == "complete" ) {
1089 // If either one are found, remove the timer
1090 clearInterval( jQuery.safariTimer );
1091 jQuery.safariTimer = null;
1093 // and execute any waiting functions
1099 // A fallback to window.onload, that will always work
1100 jQuery.event.add( window, "load", jQuery.ready );
1104 // Clean up after IE to avoid memory leaks
1105 if (jQuery.browser.msie) jQuery(window).unload(function() {
1106 var event = jQuery.event, global = event.global;
1107 for (var type in global) {
1108 var els = global[type], i = els.length;
1109 if (i>0) do if (type != 'unload') event.remove(els[i-1], type); while (--i);