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 jQuery.type(obj) === "function";
444 isArray: Array.isArray || function( obj ) {
445 return jQuery.type(obj) === "array";
448 type: function( obj ) {
451 toString.call(obj).slice(8, -1).toLowerCase();
454 isPlainObject: function( obj ) {
455 // Must be an Object.
456 // Because of IE, we also have to check the presence of the constructor property.
457 // Make sure that DOM nodes and window objects don't pass through, as well
458 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || obj.setInterval ) {
462 // Not own constructor property must be Object
463 if ( obj.constructor &&
464 !hasOwn.call(obj, "constructor") &&
465 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
469 // Own properties are enumerated firstly, so to speed up,
470 // if last one is own, then all properties are own.
473 for ( key in obj ) {}
475 return key === undefined || hasOwn.call( obj, key );
478 isEmptyObject: function( obj ) {
479 for ( var name in obj ) {
485 error: function( msg ) {
489 parseJSON: function( data ) {
490 if ( typeof data !== "string" || !data ) {
494 // Make sure leading/trailing whitespace is removed (IE can't handle it)
495 data = jQuery.trim( data );
497 // Make sure the incoming data is actual JSON
498 // Logic borrowed from http://json.org/json2.js
499 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
500 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
501 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
503 // Try to use the native JSON parser first
504 return window.JSON && window.JSON.parse ?
505 window.JSON.parse( data ) :
506 (new Function("return " + data))();
509 jQuery.error( "Invalid JSON: " + data );
515 // Evalulates a script in a global context
516 globalEval: function( data ) {
517 if ( data && rnotwhite.test(data) ) {
518 // Inspired by code by Andrea Giammarchi
519 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
520 var head = document.getElementsByTagName("head")[0] || document.documentElement,
521 script = document.createElement("script");
523 script.type = "text/javascript";
525 if ( jQuery.support.scriptEval ) {
526 script.appendChild( document.createTextNode( data ) );
531 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
532 // This arises when a base node is used (#2709).
533 head.insertBefore( script, head.firstChild );
534 head.removeChild( script );
538 nodeName: function( elem, name ) {
539 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
542 // args is for internal usage only
543 each: function( object, callback, args ) {
545 length = object.length,
546 isObj = length === undefined || jQuery.isFunction(object);
550 for ( name in object ) {
551 if ( callback.apply( object[ name ], args ) === false ) {
556 for ( ; i < length; ) {
557 if ( callback.apply( object[ i++ ], args ) === false ) {
563 // A special, fast, case for the most common use of each
566 for ( name in object ) {
567 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
572 for ( var value = object[0];
573 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
580 // Use native String.trim function wherever possible
583 return text == null ?
588 // Otherwise use our own trimming functionality
590 return text == null ?
592 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
595 // results is for internal usage only
596 makeArray: function( array, results ) {
597 var ret = results || [];
599 if ( array != null ) {
600 // The window, strings (and functions) also have 'length'
601 // The extra typeof function check is to prevent crashes
602 // in Safari 2 (See: #3039)
603 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
604 var type = jQuery.type(array);
606 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || (typeof type !== "function" && array.setInterval) ) {
607 push.call( ret, array );
609 jQuery.merge( ret, array );
616 inArray: function( elem, array ) {
617 if ( array.indexOf ) {
618 return array.indexOf( elem );
621 for ( var i = 0, length = array.length; i < length; i++ ) {
622 if ( array[ i ] === elem ) {
630 merge: function( first, second ) {
631 var i = first.length, j = 0;
633 if ( typeof second.length === "number" ) {
634 for ( var l = second.length; j < l; j++ ) {
635 first[ i++ ] = second[ j ];
639 while ( second[j] !== undefined ) {
640 first[ i++ ] = second[ j++ ];
649 grep: function( elems, callback, inv ) {
650 var ret = [], retVal;
653 // Go through the array, only saving the items
654 // that pass the validator function
655 for ( var i = 0, length = elems.length; i < length; i++ ) {
656 retVal = !!callback( elems[ i ], i );
657 if ( inv !== retVal ) {
658 ret.push( elems[ i ] );
665 // arg is for internal usage only
666 map: function( elems, callback, arg ) {
669 // Go through the array, translating each of the items to their
670 // new value (or values).
671 for ( var i = 0, length = elems.length; i < length; i++ ) {
672 value = callback( elems[ i ], i, arg );
674 if ( value != null ) {
675 ret[ ret.length ] = value;
679 return ret.concat.apply( [], ret );
682 // A global GUID counter for objects
685 proxy: function( fn, proxy, thisObject ) {
686 if ( arguments.length === 2 ) {
687 if ( typeof proxy === "string" ) {
689 fn = thisObject[ proxy ];
692 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
698 if ( !proxy && fn ) {
700 return fn.apply( thisObject || this, arguments );
704 // Set the guid of unique handler to the same of original handler, so it can be removed
706 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
709 // So proxy can be declared as an argument
713 // Mutifunctional method to get and set values to a collection
714 // The value/s can be optionally by executed if its a function
715 access: function( elems, key, value, exec, fn, pass ) {
716 var length = elems.length;
718 // Setting many attributes
719 if ( typeof key === "object" ) {
720 for ( var k in key ) {
721 jQuery.access( elems, k, key[k], exec, fn, value );
726 // Setting one attribute
727 if ( value !== undefined ) {
728 // Optionally, function values get executed if exec is true
729 exec = !pass && exec && jQuery.isFunction(value);
731 for ( var i = 0; i < length; i++ ) {
732 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
738 // Getting an attribute
739 return length ? fn( elems[0], key ) : undefined;
743 return (new Date()).getTime();
746 // Use of jQuery.browser is frowned upon.
747 // More details: http://docs.jquery.com/Utilities/jQuery.browser
748 uaMatch: function( ua ) {
749 ua = ua.toLowerCase();
751 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
752 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
753 /(msie) ([\w.]+)/.exec( ua ) ||
754 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
757 return { browser: match[1] || "", version: match[2] || "0" };
763 browserMatch = jQuery.uaMatch( userAgent );
764 if ( browserMatch.browser ) {
765 jQuery.browser[ browserMatch.browser ] = true;
766 jQuery.browser.version = browserMatch.version;
769 // Deprecated, use jQuery.browser.webkit instead
770 if ( jQuery.browser.webkit ) {
771 jQuery.browser.safari = true;
775 jQuery.inArray = function( elem, array ) {
776 return indexOf.call( array, elem );
780 // Verify that \s matches non-breaking spaces
781 // (IE fails on this test)
782 if ( !/\s/.test( "\xA0" ) ) {
783 trimLeft = /^[\s\xA0]+/;
784 trimRight = /[\s\xA0]+$/;
787 // All jQuery objects should point back to these
788 rootjQuery = jQuery(document);
790 // Cleanup functions for the document ready method
791 if ( document.addEventListener ) {
792 DOMContentLoaded = function() {
793 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
797 } else if ( document.attachEvent ) {
798 DOMContentLoaded = function() {
799 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
800 if ( document.readyState === "complete" ) {
801 document.detachEvent( "onreadystatechange", DOMContentLoaded );
807 // The DOM ready check for Internet Explorer
808 function doScrollCheck() {
809 if ( jQuery.isReady ) {
814 // If IE is used, use the trick by Diego Perini
815 // http://javascript.nwbox.com/IEContentLoaded/
816 document.documentElement.doScroll("left");
818 setTimeout( doScrollCheck, 1 );
822 // and execute any waiting functions
826 // Expose jQuery to the global object
827 window.jQuery = window.$ = jQuery;