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 );
9 // Map over jQuery in case of overwrite
10 _jQuery = window.jQuery,
12 // Map over the $ in case of overwrite
15 // Use the correct document accordingly with window argument (sandbox)
16 //document = window.document,
18 // A central reference to the root jQuery(document)
21 // A simple way to check for HTML strings or ID strings
22 // (both of which we optimize for)
23 quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w\-]+)$/,
25 // Is it a simple selector
26 isSimple = /^.[^:#\[\.,]*$/,
28 // Check if a string has a non-whitespace character in it
31 // Used for trimming whitespace
35 // Match a standalone tag
36 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
38 // Keep a UserAgent string for use with jQuery.browser
39 userAgent = navigator.userAgent,
41 // For matching the engine and version of the browser
44 // Has the ready events already been bound?
47 // The functions to execute on DOM ready
50 // The ready event handler
53 // Save a reference to some core methods
54 toString = Object.prototype.toString,
55 hasOwn = Object.prototype.hasOwnProperty,
56 push = Array.prototype.push,
57 slice = Array.prototype.slice,
58 trim = String.prototype.trim,
59 indexOf = Array.prototype.indexOf;
61 jQuery.fn = jQuery.prototype = {
62 init: function( selector, context ) {
63 var match, elem, ret, doc;
65 // Handle $(""), $(null), or $(undefined)
70 // Handle $(DOMElement)
71 if ( selector.nodeType ) {
72 this.context = this[0] = selector;
77 // The body element only exists once, optimize finding it
78 if ( selector === "body" && !context ) {
79 this.context = document;
80 this[0] = document.body;
81 this.selector = "body";
86 // Handle HTML strings
87 if ( typeof selector === "string" ) {
88 // Are we dealing with HTML string or an ID?
89 match = quickExpr.exec( selector );
91 // Verify a match, and that no context was specified for #id
92 if ( match && (match[1] || !context) ) {
94 // HANDLE: $(html) -> $(array)
96 doc = (context ? context.ownerDocument || context : document);
98 // If a single string is passed in and it's a single tag
99 // just do a createElement and skip the rest
100 ret = rsingleTag.exec( selector );
103 if ( jQuery.isPlainObject( context ) ) {
104 selector = [ document.createElement( ret[1] ) ];
105 jQuery.fn.attr.call( selector, context, true );
108 selector = [ doc.createElement( ret[1] ) ];
112 ret = buildFragment( [ match[1] ], [ doc ] );
113 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
116 return jQuery.merge( this, selector );
120 elem = document.getElementById( match[2] );
123 // Handle the case where IE and Opera return items
124 // by name instead of ID
125 if ( elem.id !== match[2] ) {
126 return rootjQuery.find( selector );
129 // Otherwise, we inject the element directly into the jQuery object
134 this.context = document;
135 this.selector = selector;
140 } else if ( !context && /^\w+$/.test( selector ) ) {
141 this.selector = selector;
142 this.context = document;
143 selector = document.getElementsByTagName( selector );
144 return jQuery.merge( this, selector );
146 // HANDLE: $(expr, $(...))
147 } else if ( !context || context.jquery ) {
148 return (context || rootjQuery).find( selector );
150 // HANDLE: $(expr, context)
151 // (which is just equivalent to: $(context).find(expr)
153 return jQuery( context ).find( selector );
156 // HANDLE: $(function)
157 // Shortcut for document ready
158 } else if ( jQuery.isFunction( selector ) ) {
159 return rootjQuery.ready( selector );
162 if (selector.selector !== undefined) {
163 this.selector = selector.selector;
164 this.context = selector.context;
167 return jQuery.makeArray( selector, this );
170 // Start with an empty selector
173 // The current version of jQuery being used
176 // The default length of a jQuery object is 0
179 // The number of elements contained in the matched element set
184 toArray: function() {
185 return slice.call( this, 0 );
188 // Get the Nth element in the matched element set OR
189 // Get the whole matched element set as a clean array
190 get: function( num ) {
193 // Return a 'clean' array
196 // Return just the object
197 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
200 // Take an array of elements and push it onto the stack
201 // (returning the new matched element set)
202 pushStack: function( elems, name, selector ) {
203 // Build a new jQuery matched element set
206 if ( jQuery.isArray( elems ) ) {
207 push.apply( ret, elems );
210 jQuery.merge( ret, elems );
213 // Add the old object onto the stack (as a reference)
214 ret.prevObject = this;
216 ret.context = this.context;
218 if ( name === "find" ) {
219 ret.selector = this.selector + (this.selector ? " " : "") + selector;
221 ret.selector = this.selector + "." + name + "(" + selector + ")";
224 // Return the newly-formed element set
228 // Execute a callback for every element in the matched set.
229 // (You can seed the arguments with an array of args, but this is
230 // only used internally.)
231 each: function( callback, args ) {
232 return jQuery.each( this, callback, args );
235 ready: function( fn ) {
236 // Attach the listeners
239 // If the DOM is already ready
240 if ( jQuery.isReady ) {
241 // Execute the function immediately
242 fn.call( document, jQuery );
244 // Otherwise, remember the function for later
245 } else if ( readyList ) {
246 // Add the function to the wait list
247 readyList.push( fn );
256 this.slice( i, +i + 1 );
264 return this.eq( -1 );
268 return this.pushStack( slice.apply( this, arguments ),
269 "slice", slice.call(arguments).join(",") );
272 map: function( callback ) {
273 return this.pushStack( jQuery.map(this, function( elem, i ) {
274 return callback.call( elem, i, elem );
279 return this.prevObject || jQuery(null);
282 // For internal use only.
283 // Behaves like an Array's method, not like a jQuery method.
289 // Give the init function the jQuery prototype for later instantiation
290 jQuery.fn.init.prototype = jQuery.fn;
292 jQuery.extend = jQuery.fn.extend = function() {
293 // copy reference to target object
294 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
296 // Handle a deep copy situation
297 if ( typeof target === "boolean" ) {
299 target = arguments[1] || {};
300 // skip the boolean and the target
304 // Handle case when target is a string or something (possible in deep copy)
305 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
309 // extend jQuery itself if only one argument is passed
310 if ( length === i ) {
315 for ( ; i < length; i++ ) {
316 // Only deal with non-null/undefined values
317 if ( (options = arguments[ i ]) != null ) {
318 // Extend the base object
319 for ( name in options ) {
320 src = target[ name ];
321 copy = options[ name ];
323 // Prevent never-ending loop
324 if ( target === copy ) {
328 // Recurse if we're merging object literal values or arrays
329 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
330 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
331 : jQuery.isArray(copy) ? [] : {};
333 // Never move original objects, clone them
334 target[ name ] = jQuery.extend( deep, clone, copy );
336 // Don't bring in undefined values
337 } else if ( copy !== undefined ) {
338 target[ name ] = copy;
344 // Return the modified object
349 noConflict: function( deep ) {
353 window.jQuery = _jQuery;
359 // Is the DOM ready to be used? Set to true once it occurs.
362 // Handle when the DOM is ready
364 // Make sure that the DOM is not already loaded
365 if ( !jQuery.isReady ) {
366 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
367 if ( !document.body ) {
368 return setTimeout( jQuery.ready, 13 );
371 // Remember that the DOM is ready
372 jQuery.isReady = true;
374 // If there are functions bound, to execute
376 // Execute all of them
378 while ( (fn = readyList[ i++ ]) ) {
379 fn.call( document, jQuery );
382 // Reset the list of functions
386 // Trigger any bound ready events
387 if ( jQuery.fn.triggerHandler ) {
388 jQuery( document ).triggerHandler( "ready" );
393 bindReady: function() {
400 // Catch cases where $(document).ready() is called after the
401 // browser event has already occurred.
402 if ( document.readyState === "complete" ) {
403 return jQuery.ready();
406 // Mozilla, Opera and webkit nightlies currently support this event
407 if ( document.addEventListener ) {
408 // Use the handy event callback
409 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
411 // A fallback to window.onload, that will always work
412 window.addEventListener( "load", jQuery.ready, false );
414 // If IE event model is used
415 } else if ( document.attachEvent ) {
416 // ensure firing before onload,
417 // maybe late but safe also for iframes
418 document.attachEvent("onreadystatechange", DOMContentLoaded);
420 // A fallback to window.onload, that will always work
421 window.attachEvent( "onload", jQuery.ready );
423 // If IE and not a frame
424 // continually check to see if the document is ready
425 var toplevel = false;
428 toplevel = window.frameElement == null;
431 if ( document.documentElement.doScroll && toplevel ) {
437 // See test/unit/core.js for details concerning isFunction.
438 // Since version 1.3, DOM methods and functions like alert
439 // aren't supported. They return false on IE (#2968).
440 isFunction: function( obj ) {
441 return toString.call(obj) === "[object Function]";
444 isArray: function( obj ) {
445 return toString.call(obj) === "[object Array]";
448 isPlainObject: function( obj ) {
449 // Must be an Object.
450 // Because of IE, we also have to check the presence of the constructor property.
451 // Make sure that DOM nodes and window objects don't pass through, as well
452 if ( !obj || toString.call(obj) !== "[object Object]" || obj.nodeType || obj.setInterval ) {
456 // Not own constructor property must be Object
457 if ( obj.constructor &&
458 !hasOwn.call(obj, "constructor") &&
459 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
463 // Own properties are enumerated firstly, so to speed up,
464 // if last one is own, then all properties are own.
467 for ( key in obj ) {}
469 return key === undefined || hasOwn.call( obj, key );
472 isEmptyObject: function( obj ) {
473 for ( var name in obj ) {
479 error: function( msg ) {
483 parseJSON: function( data ) {
484 if ( typeof data !== "string" || !data ) {
488 // Make sure leading/trailing whitespace is removed (IE can't handle it)
489 data = jQuery.trim( data );
491 // Make sure the incoming data is actual JSON
492 // Logic borrowed from http://json.org/json2.js
493 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
494 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
495 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
497 // Try to use the native JSON parser first
498 return window.JSON && window.JSON.parse ?
499 window.JSON.parse( data ) :
500 (new Function("return " + data))();
503 jQuery.error( "Invalid JSON: " + data );
509 // Evalulates a script in a global context
510 globalEval: function( data ) {
511 if ( data && rnotwhite.test(data) ) {
512 // Inspired by code by Andrea Giammarchi
513 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
514 var head = document.getElementsByTagName("head")[0] || document.documentElement,
515 script = document.createElement("script");
517 script.type = "text/javascript";
519 if ( jQuery.support.scriptEval ) {
520 script.appendChild( document.createTextNode( data ) );
525 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
526 // This arises when a base node is used (#2709).
527 head.insertBefore( script, head.firstChild );
528 head.removeChild( script );
532 nodeName: function( elem, name ) {
533 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
536 // args is for internal usage only
537 each: function( object, callback, args ) {
539 length = object.length,
540 isObj = length === undefined || jQuery.isFunction(object);
544 for ( name in object ) {
545 if ( callback.apply( object[ name ], args ) === false ) {
550 for ( ; i < length; ) {
551 if ( callback.apply( object[ i++ ], args ) === false ) {
557 // A special, fast, case for the most common use of each
560 for ( name in object ) {
561 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
566 for ( var value = object[0];
567 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
574 // Use native String.trim function wherever possible
577 return text == null ?
582 // Otherwise use our own trimming functionality
584 return text == null ?
586 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
589 // results is for internal usage only
590 makeArray: function( array, results ) {
591 var ret = results || [];
593 if ( array != null ) {
594 // The window, strings (and functions) also have 'length'
595 // The extra typeof function check is to prevent crashes
596 // in Safari 2 (See: #3039)
597 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
598 push.call( ret, array );
600 jQuery.merge( ret, array );
607 inArray: function( elem, array ) {
608 if ( array.indexOf ) {
609 return array.indexOf( elem );
612 for ( var i = 0, length = array.length; i < length; i++ ) {
613 if ( array[ i ] === elem ) {
621 merge: function( first, second ) {
622 var i = first.length, j = 0;
624 if ( typeof second.length === "number" ) {
625 for ( var l = second.length; j < l; j++ ) {
626 first[ i++ ] = second[ j ];
630 while ( second[j] !== undefined ) {
631 first[ i++ ] = second[ j++ ];
640 grep: function( elems, callback, inv ) {
641 var ret = [], retVal;
644 // Go through the array, only saving the items
645 // that pass the validator function
646 for ( var i = 0, length = elems.length; i < length; i++ ) {
647 retVal = !!callback( elems[ i ], i );
648 if ( inv !== retVal ) {
649 ret.push( elems[ i ] );
656 // arg is for internal usage only
657 map: function( elems, callback, arg ) {
660 // Go through the array, translating each of the items to their
661 // new value (or values).
662 for ( var i = 0, length = elems.length; i < length; i++ ) {
663 value = callback( elems[ i ], i, arg );
665 if ( value != null ) {
666 ret[ ret.length ] = value;
670 return ret.concat.apply( [], ret );
673 // A global GUID counter for objects
676 proxy: function( fn, proxy, thisObject ) {
677 if ( arguments.length === 2 ) {
678 if ( typeof proxy === "string" ) {
680 fn = thisObject[ proxy ];
683 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
689 if ( !proxy && fn ) {
691 return fn.apply( thisObject || this, arguments );
695 // Set the guid of unique handler to the same of original handler, so it can be removed
697 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
700 // So proxy can be declared as an argument
704 // Mutifunctional method to get and set values to a collection
705 // The value/s can be optionally by executed if its a function
706 access: function( elems, key, value, exec, fn, pass ) {
707 var length = elems.length;
709 // Setting many attributes
710 if ( typeof key === "object" ) {
711 for ( var k in key ) {
712 jQuery.access( elems, k, key[k], exec, fn, value );
717 // Setting one attribute
718 if ( value !== undefined ) {
719 // Optionally, function values get executed if exec is true
720 exec = !pass && exec && jQuery.isFunction(value);
722 for ( var i = 0; i < length; i++ ) {
723 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
729 // Getting an attribute
730 return length ? fn( elems[0], key ) : undefined;
734 return (new Date()).getTime();
737 // Use of jQuery.browser is frowned upon.
738 // More details: http://docs.jquery.com/Utilities/jQuery.browser
739 uaMatch: function( ua ) {
740 ua = ua.toLowerCase();
742 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
743 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
744 /(msie) ([\w.]+)/.exec( ua ) ||
745 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
748 return { browser: match[1] || "", version: match[2] || "0" };
754 browserMatch = jQuery.uaMatch( userAgent );
755 if ( browserMatch.browser ) {
756 jQuery.browser[ browserMatch.browser ] = true;
757 jQuery.browser.version = browserMatch.version;
760 // Deprecated, use jQuery.browser.webkit instead
761 if ( jQuery.browser.webkit ) {
762 jQuery.browser.safari = true;
766 jQuery.inArray = function( elem, array ) {
767 return indexOf.call( array, elem );
771 // Verify that \s matches non-breaking spaces
772 // (IE fails on this test)
773 if ( !/\s/.test( "\xA0" ) ) {
774 trimLeft = /^[\s\xA0]+/;
775 trimRight = /[\s\xA0]+$/;
778 // All jQuery objects should point back to these
779 rootjQuery = jQuery(document);
781 // Cleanup functions for the document ready method
782 if ( document.addEventListener ) {
783 DOMContentLoaded = function() {
784 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
788 } else if ( document.attachEvent ) {
789 DOMContentLoaded = function() {
790 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
791 if ( document.readyState === "complete" ) {
792 document.detachEvent( "onreadystatechange", DOMContentLoaded );
798 // The DOM ready check for Internet Explorer
799 function doScrollCheck() {
800 if ( jQuery.isReady ) {
805 // If IE is used, use the trick by Diego Perini
806 // http://javascript.nwbox.com/IEContentLoaded/
807 document.documentElement.doScroll("left");
809 setTimeout( doScrollCheck, 1 );
813 // and execute any waiting functions
817 // Expose jQuery to the global object
818 window.jQuery = window.$ = jQuery;