1 var jQuery = (function() {
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 // A central reference to the root jQuery(document)
18 // A simple way to check for HTML strings or ID strings
19 // (both of which we optimize for)
20 quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
22 // Is it a simple selector
23 isSimple = /^.[^:#\[\.,]*$/,
25 // Check if a string has a non-whitespace character in it
29 // Used for trimming whitespace
33 // Check for non-word characters
36 // Match a standalone tag
37 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
40 rvalidchars = /^[\],:{}\s]*$/,
41 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
42 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
43 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
46 rwebkit = /(webkit)[ \/]([\w.]+)/,
47 ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
48 rmsie = /(msie) ([\w.]+)/,
49 rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
51 // Keep a UserAgent string for use with jQuery.browser
52 userAgent = navigator.userAgent,
54 // For matching the engine and version of the browser
57 // Has the ready events already been bound?
60 // The functions to execute on DOM ready
63 // The ready event handler
66 // Save a reference to some core methods
67 toString = Object.prototype.toString,
68 hasOwn = Object.prototype.hasOwnProperty,
69 push = Array.prototype.push,
70 slice = Array.prototype.slice,
71 trim = String.prototype.trim,
72 indexOf = Array.prototype.indexOf;
74 jQuery.fn = jQuery.prototype = {
75 init: function( selector, context ) {
76 var match, elem, ret, doc;
78 // Handle $(""), $(null), or $(undefined)
83 // Handle $(DOMElement)
84 if ( selector.nodeType ) {
85 this.context = this[0] = selector;
90 // The body element only exists once, optimize finding it
91 if ( selector === "body" && !context ) {
92 this.context = document;
93 this[0] = document.body;
94 this.selector = "body";
99 // Handle HTML strings
100 if ( typeof selector === "string" ) {
101 // Are we dealing with HTML string or an ID?
102 match = quickExpr.exec( selector );
104 // Verify a match, and that no context was specified for #id
105 if ( match && (match[1] || !context) ) {
107 // HANDLE: $(html) -> $(array)
109 doc = (context ? context.ownerDocument || context : document);
111 // If a single string is passed in and it's a single tag
112 // just do a createElement and skip the rest
113 ret = rsingleTag.exec( selector );
116 if ( jQuery.isPlainObject( context ) ) {
117 selector = [ document.createElement( ret[1] ) ];
118 jQuery.fn.attr.call( selector, context, true );
121 selector = [ doc.createElement( ret[1] ) ];
125 ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
126 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
129 return jQuery.merge( this, selector );
133 elem = document.getElementById( match[2] );
135 // Check parentNode to catch when Blackberry 4.6 returns
136 // nodes that are no longer in the document #6963
137 if ( elem && elem.parentNode ) {
138 // Handle the case where IE and Opera return items
139 // by name instead of ID
140 if ( elem.id !== match[2] ) {
141 return rootjQuery.find( selector );
144 // Otherwise, we inject the element directly into the jQuery object
149 this.context = document;
150 this.selector = selector;
155 } else if ( !context && !rnonword.test( selector ) ) {
156 this.selector = selector;
157 this.context = document;
158 selector = document.getElementsByTagName( selector );
159 return jQuery.merge( this, selector );
161 // HANDLE: $(expr, $(...))
162 } else if ( !context || context.jquery ) {
163 return (context || rootjQuery).find( selector );
165 // HANDLE: $(expr, context)
166 // (which is just equivalent to: $(context).find(expr)
168 return jQuery( context ).find( selector );
171 // HANDLE: $(function)
172 // Shortcut for document ready
173 } else if ( jQuery.isFunction( selector ) ) {
174 return rootjQuery.ready( selector );
177 if (selector.selector !== undefined) {
178 this.selector = selector.selector;
179 this.context = selector.context;
182 return jQuery.makeArray( selector, this );
185 // Start with an empty selector
188 // The current version of jQuery being used
191 // The default length of a jQuery object is 0
194 // The number of elements contained in the matched element set
199 toArray: function() {
200 return slice.call( this, 0 );
203 // Get the Nth element in the matched element set OR
204 // Get the whole matched element set as a clean array
205 get: function( num ) {
208 // Return a 'clean' array
211 // Return just the object
212 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
215 // Take an array of elements and push it onto the stack
216 // (returning the new matched element set)
217 pushStack: function( elems, name, selector ) {
218 // Build a new jQuery matched element set
221 if ( jQuery.isArray( elems ) ) {
222 push.apply( ret, elems );
225 jQuery.merge( ret, elems );
228 // Add the old object onto the stack (as a reference)
229 ret.prevObject = this;
231 ret.context = this.context;
233 if ( name === "find" ) {
234 ret.selector = this.selector + (this.selector ? " " : "") + selector;
236 ret.selector = this.selector + "." + name + "(" + selector + ")";
239 // Return the newly-formed element set
243 // Execute a callback for every element in the matched set.
244 // (You can seed the arguments with an array of args, but this is
245 // only used internally.)
246 each: function( callback, args ) {
247 return jQuery.each( this, callback, args );
250 ready: function( fn ) {
251 // Attach the listeners
254 // If the DOM is already ready
255 if ( jQuery.isReady ) {
256 // Execute the function immediately
257 fn.call( document, jQuery );
259 // Otherwise, remember the function for later
260 } else if ( readyList ) {
261 // Add the function to the wait list
262 readyList.push( fn );
271 this.slice( i, +i + 1 );
279 return this.eq( -1 );
283 return this.pushStack( slice.apply( this, arguments ),
284 "slice", slice.call(arguments).join(",") );
287 map: function( callback ) {
288 return this.pushStack( jQuery.map(this, function( elem, i ) {
289 return callback.call( elem, i, elem );
294 return this.prevObject || jQuery(null);
297 // For internal use only.
298 // Behaves like an Array's method, not like a jQuery method.
304 // Give the init function the jQuery prototype for later instantiation
305 jQuery.fn.init.prototype = jQuery.fn;
307 jQuery.extend = jQuery.fn.extend = function() {
308 // copy reference to target object
309 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
311 // Handle a deep copy situation
312 if ( typeof target === "boolean" ) {
314 target = arguments[1] || {};
315 // skip the boolean and the target
319 // Handle case when target is a string or something (possible in deep copy)
320 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
324 // extend jQuery itself if only one argument is passed
325 if ( length === i ) {
330 for ( ; i < length; i++ ) {
331 // Only deal with non-null/undefined values
332 if ( (options = arguments[ i ]) != null ) {
333 // Extend the base object
334 for ( name in options ) {
335 src = target[ name ];
336 copy = options[ name ];
338 // Prevent never-ending loop
339 if ( target === copy ) {
343 // Recurse if we're merging object literal values or arrays
344 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
345 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
346 : jQuery.isArray(copy) ? [] : {};
348 // Never move original objects, clone them
349 target[ name ] = jQuery.extend( deep, clone, copy );
351 // Don't bring in undefined values
352 } else if ( copy !== undefined ) {
353 target[ name ] = copy;
359 // Return the modified object
364 noConflict: function( deep ) {
368 window.jQuery = _jQuery;
374 // Is the DOM ready to be used? Set to true once it occurs.
377 // A counter to track how many items to wait for before
378 // the ready event fires. See #6781
381 // Handle when the DOM is ready
382 ready: function( wait ) {
383 // A third-party is pushing the ready event forwards
384 if ( wait === true ) {
388 // Make sure that the DOM is not already loaded
389 if ( !jQuery.readyWait || (wait !== true && !jQuery.isReady) ) {
390 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
391 if ( !document.body ) {
392 return setTimeout( jQuery.ready, 13 );
395 // Remember that the DOM is ready
396 jQuery.isReady = true;
398 // If a normal DOM Ready event fired, decrement, and wait if need be
399 if ( wait !== true && --jQuery.readyWait > 0 ) {
403 // If there are functions bound, to execute
405 // Execute all of them
407 while ( (fn = readyList[ i++ ]) ) {
408 fn.call( document, jQuery );
411 // Reset the list of functions
415 // Trigger any bound ready events
416 if ( jQuery.fn.triggerHandler ) {
417 jQuery( document ).triggerHandler( "ready" );
422 bindReady: function() {
429 // Catch cases where $(document).ready() is called after the
430 // browser event has already occurred.
431 if ( document.readyState === "complete" ) {
432 return jQuery.ready();
435 // Mozilla, Opera and webkit nightlies currently support this event
436 if ( document.addEventListener ) {
437 // Use the handy event callback
438 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
440 // A fallback to window.onload, that will always work
441 window.addEventListener( "load", jQuery.ready, false );
443 // If IE event model is used
444 } else if ( document.attachEvent ) {
445 // ensure firing before onload,
446 // maybe late but safe also for iframes
447 document.attachEvent("onreadystatechange", DOMContentLoaded);
449 // A fallback to window.onload, that will always work
450 window.attachEvent( "onload", jQuery.ready );
452 // If IE and not a frame
453 // continually check to see if the document is ready
454 var toplevel = false;
457 toplevel = window.frameElement == null;
460 if ( document.documentElement.doScroll && toplevel ) {
466 // See test/unit/core.js for details concerning isFunction.
467 // Since version 1.3, DOM methods and functions like alert
468 // aren't supported. They return false on IE (#2968).
469 isFunction: function( obj ) {
470 return jQuery.type(obj) === "function";
473 isArray: Array.isArray || function( obj ) {
474 return jQuery.type(obj) === "array";
477 // A crude way of determining if an object is a window
478 isWindow: function( obj ) {
479 return obj && typeof obj === "object" && "setInterval" in obj;
482 type: function( obj ) {
485 toString.call(obj).slice(8, -1).toLowerCase();
488 isPlainObject: function( obj ) {
489 // Must be an Object.
490 // Because of IE, we also have to check the presence of the constructor property.
491 // Make sure that DOM nodes and window objects don't pass through, as well
492 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
496 // Not own constructor property must be Object
497 if ( obj.constructor &&
498 !hasOwn.call(obj, "constructor") &&
499 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
503 // Own properties are enumerated firstly, so to speed up,
504 // if last one is own, then all properties are own.
507 for ( key in obj ) {}
509 return key === undefined || hasOwn.call( obj, key );
512 isEmptyObject: function( obj ) {
513 for ( var name in obj ) {
519 error: function( msg ) {
523 parseJSON: function( data ) {
524 if ( typeof data !== "string" || !data ) {
528 // Make sure leading/trailing whitespace is removed (IE can't handle it)
529 data = jQuery.trim( data );
531 // Make sure the incoming data is actual JSON
532 // Logic borrowed from http://json.org/json2.js
533 if ( rvalidchars.test(data.replace(rvalidescape, "@")
534 .replace(rvalidtokens, "]")
535 .replace(rvalidbraces, "")) ) {
537 // Try to use the native JSON parser first
538 return window.JSON && window.JSON.parse ?
539 window.JSON.parse( data ) :
540 (new Function("return " + data))();
543 jQuery.error( "Invalid JSON: " + data );
549 // Evalulates a script in a global context
550 globalEval: function( data ) {
551 if ( data && rnotwhite.test(data) ) {
552 // Inspired by code by Andrea Giammarchi
553 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
554 var head = document.getElementsByTagName("head")[0] || document.documentElement,
555 script = document.createElement("script");
557 script.type = "text/javascript";
559 if ( jQuery.support.scriptEval ) {
560 script.appendChild( document.createTextNode( data ) );
565 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
566 // This arises when a base node is used (#2709).
567 head.insertBefore( script, head.firstChild );
568 head.removeChild( script );
572 nodeName: function( elem, name ) {
573 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
576 // args is for internal usage only
577 each: function( object, callback, args ) {
579 length = object.length,
580 isObj = length === undefined || jQuery.isFunction(object);
584 for ( name in object ) {
585 if ( callback.apply( object[ name ], args ) === false ) {
590 for ( ; i < length; ) {
591 if ( callback.apply( object[ i++ ], args ) === false ) {
597 // A special, fast, case for the most common use of each
600 for ( name in object ) {
601 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
606 for ( var value = object[0];
607 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
614 // Use native String.trim function wherever possible
617 return text == null ?
622 // Otherwise use our own trimming functionality
624 return text == null ?
626 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
629 // results is for internal usage only
630 makeArray: function( array, results ) {
631 var ret = results || [];
633 if ( array != null ) {
634 // The window, strings (and functions) also have 'length'
635 // The extra typeof function check is to prevent crashes
636 // in Safari 2 (See: #3039)
637 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
638 var type = jQuery.type(array);
640 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) ) {
641 push.call( ret, array );
643 jQuery.merge( ret, array );
650 inArray: function( elem, array ) {
651 if ( array.indexOf ) {
652 return array.indexOf( elem );
655 for ( var i = 0, length = array.length; i < length; i++ ) {
656 if ( array[ i ] === elem ) {
664 merge: function( first, second ) {
665 var i = first.length, j = 0;
667 if ( typeof second.length === "number" ) {
668 for ( var l = second.length; j < l; j++ ) {
669 first[ i++ ] = second[ j ];
673 while ( second[j] !== undefined ) {
674 first[ i++ ] = second[ j++ ];
683 grep: function( elems, callback, inv ) {
684 var ret = [], retVal;
687 // Go through the array, only saving the items
688 // that pass the validator function
689 for ( var i = 0, length = elems.length; i < length; i++ ) {
690 retVal = !!callback( elems[ i ], i );
691 if ( inv !== retVal ) {
692 ret.push( elems[ i ] );
699 // arg is for internal usage only
700 map: function( elems, callback, arg ) {
703 // Go through the array, translating each of the items to their
704 // new value (or values).
705 for ( var i = 0, length = elems.length; i < length; i++ ) {
706 value = callback( elems[ i ], i, arg );
708 if ( value != null ) {
709 ret[ ret.length ] = value;
713 return ret.concat.apply( [], ret );
716 // A global GUID counter for objects
719 proxy: function( fn, proxy, thisObject ) {
720 if ( arguments.length === 2 ) {
721 if ( typeof proxy === "string" ) {
723 fn = thisObject[ proxy ];
726 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
732 if ( !proxy && fn ) {
734 return fn.apply( thisObject || this, arguments );
738 // Set the guid of unique handler to the same of original handler, so it can be removed
740 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
743 // So proxy can be declared as an argument
747 // Mutifunctional method to get and set values to a collection
748 // The value/s can be optionally by executed if its a function
749 access: function( elems, key, value, exec, fn, pass ) {
750 var length = elems.length;
752 // Setting many attributes
753 if ( typeof key === "object" ) {
754 for ( var k in key ) {
755 jQuery.access( elems, k, key[k], exec, fn, value );
760 // Setting one attribute
761 if ( value !== undefined ) {
762 // Optionally, function values get executed if exec is true
763 exec = !pass && exec && jQuery.isFunction(value);
765 for ( var i = 0; i < length; i++ ) {
766 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
772 // Getting an attribute
773 return length ? fn( elems[0], key ) : undefined;
777 return (new Date()).getTime();
780 // Use of jQuery.browser is frowned upon.
781 // More details: http://docs.jquery.com/Utilities/jQuery.browser
782 uaMatch: function( ua ) {
783 ua = ua.toLowerCase();
785 var match = rwebkit.exec( ua ) ||
788 ua.indexOf("compatible") < 0 && rmozilla.exec( ua ) ||
791 return { browser: match[1] || "", version: match[2] || "0" };
797 browserMatch = jQuery.uaMatch( userAgent );
798 if ( browserMatch.browser ) {
799 jQuery.browser[ browserMatch.browser ] = true;
800 jQuery.browser.version = browserMatch.version;
803 // Deprecated, use jQuery.browser.webkit instead
804 if ( jQuery.browser.webkit ) {
805 jQuery.browser.safari = true;
809 jQuery.inArray = function( elem, array ) {
810 return indexOf.call( array, elem );
814 // Verify that \s matches non-breaking spaces
815 // (IE fails on this test)
816 if ( !rwhite.test( "\xA0" ) ) {
817 trimLeft = /^[\s\xA0]+/;
818 trimRight = /[\s\xA0]+$/;
821 // All jQuery objects should point back to these
822 rootjQuery = jQuery(document);
824 // Cleanup functions for the document ready method
825 if ( document.addEventListener ) {
826 DOMContentLoaded = function() {
827 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
831 } else if ( document.attachEvent ) {
832 DOMContentLoaded = function() {
833 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
834 if ( document.readyState === "complete" ) {
835 document.detachEvent( "onreadystatechange", DOMContentLoaded );
841 // The DOM ready check for Internet Explorer
842 function doScrollCheck() {
843 if ( jQuery.isReady ) {
848 // If IE is used, use the trick by Diego Perini
849 // http://javascript.nwbox.com/IEContentLoaded/
850 document.documentElement.doScroll("left");
852 setTimeout( doScrollCheck, 1 );
856 // and execute any waiting functions
860 // Expose jQuery to the global object
861 return (window.jQuery = window.$ = jQuery);