Re-work the document scoping.
[jquery.git] / src / core.js
1 var jQuery = (function() {
2
3 // Define a local copy of jQuery
4 var jQuery = function( selector, context ) {
5                 // The jQuery object is actually just the init constructor 'enhanced'
6                 return new jQuery.fn.init( selector, context );
7         },
8
9         // Map over jQuery in case of overwrite
10         _jQuery = window.jQuery,
11
12         // Map over the $ in case of overwrite
13         _$ = window.$,
14
15         // A central reference to the root jQuery(document)
16         rootjQuery,
17
18         // A simple way to check for HTML strings or ID strings
19         // (both of which we optimize for)
20         quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w\-]+)$/,
21
22         // Is it a simple selector
23         isSimple = /^.[^:#\[\.,]*$/,
24
25         // Check if a string has a non-whitespace character in it
26         rnotwhite = /\S/,
27
28         // Used for trimming whitespace
29         trimLeft = /^\s+/,
30         trimRight = /\s+$/,
31
32         // Match a standalone tag
33         rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
34
35         // Keep a UserAgent string for use with jQuery.browser
36         userAgent = navigator.userAgent,
37
38         // For matching the engine and version of the browser
39         browserMatch,
40         
41         // Has the ready events already been bound?
42         readyBound = false,
43         
44         // The functions to execute on DOM ready
45         readyList = [],
46
47         // The ready event handler
48         DOMContentLoaded,
49
50         // Save a reference to some core methods
51         toString = Object.prototype.toString,
52         hasOwn = Object.prototype.hasOwnProperty,
53         push = Array.prototype.push,
54         slice = Array.prototype.slice,
55         trim = String.prototype.trim,
56         indexOf = Array.prototype.indexOf;
57
58 jQuery.fn = jQuery.prototype = {
59         init: function( selector, context ) {
60                 var match, elem, ret, doc;
61
62                 // Handle $(""), $(null), or $(undefined)
63                 if ( !selector ) {
64                         return this;
65                 }
66
67                 // Handle $(DOMElement)
68                 if ( selector.nodeType ) {
69                         this.context = this[0] = selector;
70                         this.length = 1;
71                         return this;
72                 }
73                 
74                 // The body element only exists once, optimize finding it
75                 if ( selector === "body" && !context ) {
76                         this.context = document;
77                         this[0] = document.body;
78                         this.selector = "body";
79                         this.length = 1;
80                         return this;
81                 }
82
83                 // Handle HTML strings
84                 if ( typeof selector === "string" ) {
85                         // Are we dealing with HTML string or an ID?
86                         match = quickExpr.exec( selector );
87
88                         // Verify a match, and that no context was specified for #id
89                         if ( match && (match[1] || !context) ) {
90
91                                 // HANDLE: $(html) -> $(array)
92                                 if ( match[1] ) {
93                                         doc = (context ? context.ownerDocument || context : document);
94
95                                         // If a single string is passed in and it's a single tag
96                                         // just do a createElement and skip the rest
97                                         ret = rsingleTag.exec( selector );
98
99                                         if ( ret ) {
100                                                 if ( jQuery.isPlainObject( context ) ) {
101                                                         selector = [ document.createElement( ret[1] ) ];
102                                                         jQuery.fn.attr.call( selector, context, true );
103
104                                                 } else {
105                                                         selector = [ doc.createElement( ret[1] ) ];
106                                                 }
107
108                                         } else {
109                                                 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
110                                                 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
111                                         }
112                                         
113                                         return jQuery.merge( this, selector );
114                                         
115                                 // HANDLE: $("#id")
116                                 } else {
117                                         elem = document.getElementById( match[2] );
118
119                                         // Check parentNode to catch when Blackberry 4.6 returns
120                                         // nodes that are no longer in the document #6963
121                                         if ( elem && elem.parentNode ) {
122                                                 // Handle the case where IE and Opera return items
123                                                 // by name instead of ID
124                                                 if ( elem.id !== match[2] ) {
125                                                         return rootjQuery.find( selector );
126                                                 }
127
128                                                 // Otherwise, we inject the element directly into the jQuery object
129                                                 this.length = 1;
130                                                 this[0] = elem;
131                                         }
132
133                                         this.context = document;
134                                         this.selector = selector;
135                                         return this;
136                                 }
137
138                         // HANDLE: $("TAG")
139                         } else if ( !context && /^\w+$/.test( selector ) ) {
140                                 this.selector = selector;
141                                 this.context = document;
142                                 selector = document.getElementsByTagName( selector );
143                                 return jQuery.merge( this, selector );
144
145                         // HANDLE: $(expr, $(...))
146                         } else if ( !context || context.jquery ) {
147                                 return (context || rootjQuery).find( selector );
148
149                         // HANDLE: $(expr, context)
150                         // (which is just equivalent to: $(context).find(expr)
151                         } else {
152                                 return jQuery( context ).find( selector );
153                         }
154
155                 // HANDLE: $(function)
156                 // Shortcut for document ready
157                 } else if ( jQuery.isFunction( selector ) ) {
158                         return rootjQuery.ready( selector );
159                 }
160
161                 if (selector.selector !== undefined) {
162                         this.selector = selector.selector;
163                         this.context = selector.context;
164                 }
165
166                 return jQuery.makeArray( selector, this );
167         },
168
169         // Start with an empty selector
170         selector: "",
171
172         // The current version of jQuery being used
173         jquery: "@VERSION",
174
175         // The default length of a jQuery object is 0
176         length: 0,
177
178         // The number of elements contained in the matched element set
179         size: function() {
180                 return this.length;
181         },
182
183         toArray: function() {
184                 return slice.call( this, 0 );
185         },
186
187         // Get the Nth element in the matched element set OR
188         // Get the whole matched element set as a clean array
189         get: function( num ) {
190                 return num == null ?
191
192                         // Return a 'clean' array
193                         this.toArray() :
194
195                         // Return just the object
196                         ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
197         },
198
199         // Take an array of elements and push it onto the stack
200         // (returning the new matched element set)
201         pushStack: function( elems, name, selector ) {
202                 // Build a new jQuery matched element set
203                 var ret = jQuery();
204
205                 if ( jQuery.isArray( elems ) ) {
206                         push.apply( ret, elems );
207                 
208                 } else {
209                         jQuery.merge( ret, elems );
210                 }
211
212                 // Add the old object onto the stack (as a reference)
213                 ret.prevObject = this;
214
215                 ret.context = this.context;
216
217                 if ( name === "find" ) {
218                         ret.selector = this.selector + (this.selector ? " " : "") + selector;
219                 } else if ( name ) {
220                         ret.selector = this.selector + "." + name + "(" + selector + ")";
221                 }
222
223                 // Return the newly-formed element set
224                 return ret;
225         },
226
227         // Execute a callback for every element in the matched set.
228         // (You can seed the arguments with an array of args, but this is
229         // only used internally.)
230         each: function( callback, args ) {
231                 return jQuery.each( this, callback, args );
232         },
233         
234         ready: function( fn ) {
235                 // Attach the listeners
236                 jQuery.bindReady();
237
238                 // If the DOM is already ready
239                 if ( jQuery.isReady ) {
240                         // Execute the function immediately
241                         fn.call( document, jQuery );
242
243                 // Otherwise, remember the function for later
244                 } else if ( readyList ) {
245                         // Add the function to the wait list
246                         readyList.push( fn );
247                 }
248
249                 return this;
250         },
251         
252         eq: function( i ) {
253                 return i === -1 ?
254                         this.slice( i ) :
255                         this.slice( i, +i + 1 );
256         },
257
258         first: function() {
259                 return this.eq( 0 );
260         },
261
262         last: function() {
263                 return this.eq( -1 );
264         },
265
266         slice: function() {
267                 return this.pushStack( slice.apply( this, arguments ),
268                         "slice", slice.call(arguments).join(",") );
269         },
270
271         map: function( callback ) {
272                 return this.pushStack( jQuery.map(this, function( elem, i ) {
273                         return callback.call( elem, i, elem );
274                 }));
275         },
276         
277         end: function() {
278                 return this.prevObject || jQuery(null);
279         },
280
281         // For internal use only.
282         // Behaves like an Array's method, not like a jQuery method.
283         push: push,
284         sort: [].sort,
285         splice: [].splice
286 };
287
288 // Give the init function the jQuery prototype for later instantiation
289 jQuery.fn.init.prototype = jQuery.fn;
290
291 jQuery.extend = jQuery.fn.extend = function() {
292         // copy reference to target object
293         var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
294
295         // Handle a deep copy situation
296         if ( typeof target === "boolean" ) {
297                 deep = target;
298                 target = arguments[1] || {};
299                 // skip the boolean and the target
300                 i = 2;
301         }
302
303         // Handle case when target is a string or something (possible in deep copy)
304         if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
305                 target = {};
306         }
307
308         // extend jQuery itself if only one argument is passed
309         if ( length === i ) {
310                 target = this;
311                 --i;
312         }
313
314         for ( ; i < length; i++ ) {
315                 // Only deal with non-null/undefined values
316                 if ( (options = arguments[ i ]) != null ) {
317                         // Extend the base object
318                         for ( name in options ) {
319                                 src = target[ name ];
320                                 copy = options[ name ];
321
322                                 // Prevent never-ending loop
323                                 if ( target === copy ) {
324                                         continue;
325                                 }
326
327                                 // Recurse if we're merging object literal values or arrays
328                                 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
329                                         var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
330                                                 : jQuery.isArray(copy) ? [] : {};
331
332                                         // Never move original objects, clone them
333                                         target[ name ] = jQuery.extend( deep, clone, copy );
334
335                                 // Don't bring in undefined values
336                                 } else if ( copy !== undefined ) {
337                                         target[ name ] = copy;
338                                 }
339                         }
340                 }
341         }
342
343         // Return the modified object
344         return target;
345 };
346
347 jQuery.extend({
348         noConflict: function( deep ) {
349                 window.$ = _$;
350
351                 if ( deep ) {
352                         window.jQuery = _jQuery;
353                 }
354
355                 return jQuery;
356         },
357         
358         // Is the DOM ready to be used? Set to true once it occurs.
359         isReady: false,
360
361         // A counter to track how many items to wait for before
362         // the ready event fires. See #6781
363         readyWait: 1,
364         
365         // Handle when the DOM is ready
366         ready: function( wait ) {
367                 // A third-party is pushing the ready event forwards
368                 if ( wait === true ) {
369                         jQuery.readyWait--;
370                 }
371
372                 // Make sure that the DOM is not already loaded
373                 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
374                         // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
375                         if ( !document.body ) {
376                                 return setTimeout( jQuery.ready, 13 );
377                         }
378
379                         // Remember that the DOM is ready
380                         jQuery.isReady = true;
381
382                         // If a normal DOM Ready event fired, decrement, and wait if need be
383                         if ( wait !== true && --jQuery.readyWait > 0 ) {
384                                 return;
385                         }
386
387                         // If there are functions bound, to execute
388                         if ( readyList ) {
389                                 // Execute all of them
390                                 var fn, i = 0;
391                                 while ( (fn = readyList[ i++ ]) ) {
392                                         fn.call( document, jQuery );
393                                 }
394
395                                 // Reset the list of functions
396                                 readyList = null;
397                         }
398
399                         // Trigger any bound ready events
400                         if ( jQuery.fn.triggerHandler ) {
401                                 jQuery( document ).triggerHandler( "ready" );
402                         }
403                 }
404         },
405         
406         bindReady: function() {
407                 if ( readyBound ) {
408                         return;
409                 }
410
411                 readyBound = true;
412
413                 // Catch cases where $(document).ready() is called after the
414                 // browser event has already occurred.
415                 if ( document.readyState === "complete" ) {
416                         return jQuery.ready();
417                 }
418
419                 // Mozilla, Opera and webkit nightlies currently support this event
420                 if ( document.addEventListener ) {
421                         // Use the handy event callback
422                         document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
423                         
424                         // A fallback to window.onload, that will always work
425                         window.addEventListener( "load", jQuery.ready, false );
426
427                 // If IE event model is used
428                 } else if ( document.attachEvent ) {
429                         // ensure firing before onload,
430                         // maybe late but safe also for iframes
431                         document.attachEvent("onreadystatechange", DOMContentLoaded);
432                         
433                         // A fallback to window.onload, that will always work
434                         window.attachEvent( "onload", jQuery.ready );
435
436                         // If IE and not a frame
437                         // continually check to see if the document is ready
438                         var toplevel = false;
439
440                         try {
441                                 toplevel = window.frameElement == null;
442                         } catch(e) {}
443
444                         if ( document.documentElement.doScroll && toplevel ) {
445                                 doScrollCheck();
446                         }
447                 }
448         },
449
450         // See test/unit/core.js for details concerning isFunction.
451         // Since version 1.3, DOM methods and functions like alert
452         // aren't supported. They return false on IE (#2968).
453         isFunction: function( obj ) {
454                 return jQuery.type(obj) === "function";
455         },
456
457         isArray: Array.isArray || function( obj ) {
458                 return jQuery.type(obj) === "array";
459         },
460
461         type: function( obj ) {
462                 return obj == null ?
463                         String( obj ) :
464                         toString.call(obj).slice(8, -1).toLowerCase();
465         },
466
467         isPlainObject: function( obj ) {
468                 // Must be an Object.
469                 // Because of IE, we also have to check the presence of the constructor property.
470                 // Make sure that DOM nodes and window objects don't pass through, as well
471                 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || obj.setInterval ) {
472                         return false;
473                 }
474                 
475                 // Not own constructor property must be Object
476                 if ( obj.constructor &&
477                         !hasOwn.call(obj, "constructor") &&
478                         !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
479                         return false;
480                 }
481                 
482                 // Own properties are enumerated firstly, so to speed up,
483                 // if last one is own, then all properties are own.
484         
485                 var key;
486                 for ( key in obj ) {}
487                 
488                 return key === undefined || hasOwn.call( obj, key );
489         },
490
491         isEmptyObject: function( obj ) {
492                 for ( var name in obj ) {
493                         return false;
494                 }
495                 return true;
496         },
497         
498         error: function( msg ) {
499                 throw msg;
500         },
501         
502         parseJSON: function( data ) {
503                 if ( typeof data !== "string" || !data ) {
504                         return null;
505                 }
506
507                 // Make sure leading/trailing whitespace is removed (IE can't handle it)
508                 data = jQuery.trim( data );
509                 
510                 // Make sure the incoming data is actual JSON
511                 // Logic borrowed from http://json.org/json2.js
512                 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
513                         .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
514                         .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
515
516                         // Try to use the native JSON parser first
517                         return window.JSON && window.JSON.parse ?
518                                 window.JSON.parse( data ) :
519                                 (new Function("return " + data))();
520
521                 } else {
522                         jQuery.error( "Invalid JSON: " + data );
523                 }
524         },
525
526         noop: function() {},
527
528         // Evalulates a script in a global context
529         globalEval: function( data ) {
530                 if ( data && rnotwhite.test(data) ) {
531                         // Inspired by code by Andrea Giammarchi
532                         // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
533                         var head = document.getElementsByTagName("head")[0] || document.documentElement,
534                                 script = document.createElement("script");
535
536                         script.type = "text/javascript";
537
538                         if ( jQuery.support.scriptEval ) {
539                                 script.appendChild( document.createTextNode( data ) );
540                         } else {
541                                 script.text = data;
542                         }
543
544                         // Use insertBefore instead of appendChild to circumvent an IE6 bug.
545                         // This arises when a base node is used (#2709).
546                         head.insertBefore( script, head.firstChild );
547                         head.removeChild( script );
548                 }
549         },
550
551         nodeName: function( elem, name ) {
552                 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
553         },
554
555         // args is for internal usage only
556         each: function( object, callback, args ) {
557                 var name, i = 0,
558                         length = object.length,
559                         isObj = length === undefined || jQuery.isFunction(object);
560
561                 if ( args ) {
562                         if ( isObj ) {
563                                 for ( name in object ) {
564                                         if ( callback.apply( object[ name ], args ) === false ) {
565                                                 break;
566                                         }
567                                 }
568                         } else {
569                                 for ( ; i < length; ) {
570                                         if ( callback.apply( object[ i++ ], args ) === false ) {
571                                                 break;
572                                         }
573                                 }
574                         }
575
576                 // A special, fast, case for the most common use of each
577                 } else {
578                         if ( isObj ) {
579                                 for ( name in object ) {
580                                         if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
581                                                 break;
582                                         }
583                                 }
584                         } else {
585                                 for ( var value = object[0];
586                                         i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
587                         }
588                 }
589
590                 return object;
591         },
592
593         // Use native String.trim function wherever possible
594         trim: trim ?
595                 function( text ) {
596                         return text == null ?
597                                 "" :
598                                 trim.call( text );
599                 } :
600
601                 // Otherwise use our own trimming functionality
602                 function( text ) {
603                         return text == null ?
604                                 "" :
605                                 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
606                 },
607
608         // results is for internal usage only
609         makeArray: function( array, results ) {
610                 var ret = results || [];
611
612                 if ( array != null ) {
613                         // The window, strings (and functions) also have 'length'
614                         // The extra typeof function check is to prevent crashes
615                         // in Safari 2 (See: #3039)
616                         // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
617                         var type = jQuery.type(array);
618
619                         if ( array.length == null || type === "string" || type === "function" || type === "regexp" || "setInterval" in array ) {
620                                 push.call( ret, array );
621                         } else {
622                                 jQuery.merge( ret, array );
623                         }
624                 }
625
626                 return ret;
627         },
628
629         inArray: function( elem, array ) {
630                 if ( array.indexOf ) {
631                         return array.indexOf( elem );
632                 }
633
634                 for ( var i = 0, length = array.length; i < length; i++ ) {
635                         if ( array[ i ] === elem ) {
636                                 return i;
637                         }
638                 }
639
640                 return -1;
641         },
642
643         merge: function( first, second ) {
644                 var i = first.length, j = 0;
645
646                 if ( typeof second.length === "number" ) {
647                         for ( var l = second.length; j < l; j++ ) {
648                                 first[ i++ ] = second[ j ];
649                         }
650                 
651                 } else {
652                         while ( second[j] !== undefined ) {
653                                 first[ i++ ] = second[ j++ ];
654                         }
655                 }
656
657                 first.length = i;
658
659                 return first;
660         },
661
662         grep: function( elems, callback, inv ) {
663                 var ret = [], retVal;
664                 inv = !!inv;
665
666                 // Go through the array, only saving the items
667                 // that pass the validator function
668                 for ( var i = 0, length = elems.length; i < length; i++ ) {
669                         retVal = !!callback( elems[ i ], i );
670                         if ( inv !== retVal ) {
671                                 ret.push( elems[ i ] );
672                         }
673                 }
674
675                 return ret;
676         },
677
678         // arg is for internal usage only
679         map: function( elems, callback, arg ) {
680                 var ret = [], value;
681
682                 // Go through the array, translating each of the items to their
683                 // new value (or values).
684                 for ( var i = 0, length = elems.length; i < length; i++ ) {
685                         value = callback( elems[ i ], i, arg );
686
687                         if ( value != null ) {
688                                 ret[ ret.length ] = value;
689                         }
690                 }
691
692                 return ret.concat.apply( [], ret );
693         },
694
695         // A global GUID counter for objects
696         guid: 1,
697
698         proxy: function( fn, proxy, thisObject ) {
699                 if ( arguments.length === 2 ) {
700                         if ( typeof proxy === "string" ) {
701                                 thisObject = fn;
702                                 fn = thisObject[ proxy ];
703                                 proxy = undefined;
704
705                         } else if ( proxy && !jQuery.isFunction( proxy ) ) {
706                                 thisObject = proxy;
707                                 proxy = undefined;
708                         }
709                 }
710
711                 if ( !proxy && fn ) {
712                         proxy = function() {
713                                 return fn.apply( thisObject || this, arguments );
714                         };
715                 }
716
717                 // Set the guid of unique handler to the same of original handler, so it can be removed
718                 if ( fn ) {
719                         proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
720                 }
721
722                 // So proxy can be declared as an argument
723                 return proxy;
724         },
725
726         // Mutifunctional method to get and set values to a collection
727         // The value/s can be optionally by executed if its a function
728         access: function( elems, key, value, exec, fn, pass ) {
729                 var length = elems.length;
730         
731                 // Setting many attributes
732                 if ( typeof key === "object" ) {
733                         for ( var k in key ) {
734                                 jQuery.access( elems, k, key[k], exec, fn, value );
735                         }
736                         return elems;
737                 }
738         
739                 // Setting one attribute
740                 if ( value !== undefined ) {
741                         // Optionally, function values get executed if exec is true
742                         exec = !pass && exec && jQuery.isFunction(value);
743                 
744                         for ( var i = 0; i < length; i++ ) {
745                                 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
746                         }
747                 
748                         return elems;
749                 }
750         
751                 // Getting an attribute
752                 return length ? fn( elems[0], key ) : undefined;
753         },
754
755         now: function() {
756                 return (new Date()).getTime();
757         },
758
759         // Use of jQuery.browser is frowned upon.
760         // More details: http://docs.jquery.com/Utilities/jQuery.browser
761         uaMatch: function( ua ) {
762                 ua = ua.toLowerCase();
763
764                 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
765                         /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
766                         /(msie) ([\w.]+)/.exec( ua ) ||
767                         !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
768                         [];
769
770                 return { browser: match[1] || "", version: match[2] || "0" };
771         },
772
773         browser: {}
774 });
775
776 browserMatch = jQuery.uaMatch( userAgent );
777 if ( browserMatch.browser ) {
778         jQuery.browser[ browserMatch.browser ] = true;
779         jQuery.browser.version = browserMatch.version;
780 }
781
782 // Deprecated, use jQuery.browser.webkit instead
783 if ( jQuery.browser.webkit ) {
784         jQuery.browser.safari = true;
785 }
786
787 if ( indexOf ) {
788         jQuery.inArray = function( elem, array ) {
789                 return indexOf.call( array, elem );
790         };
791 }
792
793 // Verify that \s matches non-breaking spaces
794 // (IE fails on this test)
795 if ( !/\s/.test( "\xA0" ) ) {
796         trimLeft = /^[\s\xA0]+/;
797         trimRight = /[\s\xA0]+$/;
798 }
799
800 // All jQuery objects should point back to these
801 rootjQuery = jQuery(document);
802
803 // Cleanup functions for the document ready method
804 if ( document.addEventListener ) {
805         DOMContentLoaded = function() {
806                 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
807                 jQuery.ready();
808         };
809
810 } else if ( document.attachEvent ) {
811         DOMContentLoaded = function() {
812                 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
813                 if ( document.readyState === "complete" ) {
814                         document.detachEvent( "onreadystatechange", DOMContentLoaded );
815                         jQuery.ready();
816                 }
817         };
818 }
819
820 // The DOM ready check for Internet Explorer
821 function doScrollCheck() {
822         if ( jQuery.isReady ) {
823                 return;
824         }
825
826         try {
827                 // If IE is used, use the trick by Diego Perini
828                 // http://javascript.nwbox.com/IEContentLoaded/
829                 document.documentElement.doScroll("left");
830         } catch(e) {
831                 setTimeout( doScrollCheck, 1 );
832                 return;
833         }
834
835         // and execute any waiting functions
836         jQuery.ready();
837 }
838
839 // Expose jQuery to the global object
840 return (window.jQuery = window.$ = jQuery);
841
842 })();