1 // Define a local copy of jQuery
2 var jQuery = function( selector, context ) {
3 // The jQuery object is actually just the init constructor 'enhanced'
4 return new jQuery.fn.init( selector, context );
7 // Map over jQuery in case of overwrite
8 _jQuery = window.jQuery,
10 // Map over the $ in case of overwrite
13 // Use the correct document accordingly with window argument (sandbox)
14 document = window.document,
16 // A central reference to the root jQuery(document)
19 // A simple way to check for HTML strings or ID strings
20 // (both of which we optimize for)
21 quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w\-]+)$/,
23 // Is it a simple selector
24 isSimple = /^.[^:#\[\.,]*$/,
26 // Check if a string has a non-whitespace character in it
29 // Used for trimming whitespace
33 // Match a standalone tag
34 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
36 // Keep a UserAgent string for use with jQuery.browser
37 userAgent = navigator.userAgent,
39 // For matching the engine and version of the browser
42 // Has the ready events already been bound?
45 // The functions to execute on DOM ready
48 // The ready event handler
51 // Save a reference to some core methods
52 toString = Object.prototype.toString,
53 hasOwn = Object.prototype.hasOwnProperty,
54 push = Array.prototype.push,
55 slice = Array.prototype.slice,
56 trim = String.prototype.trim,
57 indexOf = Array.prototype.indexOf;
59 jQuery.fn = jQuery.prototype = {
60 init: function( selector, context ) {
61 var match, elem, ret, doc;
63 // Handle $(""), $(null), or $(undefined)
68 // Handle $(DOMElement)
69 if ( selector.nodeType ) {
70 this.context = this[0] = selector;
75 // The body element only exists once, optimize finding it
76 if ( selector === "body" && !context ) {
77 this.context = document;
78 this[0] = document.body;
79 this.selector = "body";
84 // Handle HTML strings
85 if ( typeof selector === "string" ) {
86 // Are we dealing with HTML string or an ID?
87 match = quickExpr.exec( selector );
89 // Verify a match, and that no context was specified for #id
90 if ( match && (match[1] || !context) ) {
92 // HANDLE: $(html) -> $(array)
94 doc = (context ? context.ownerDocument || context : document);
96 // If a single string is passed in and it's a single tag
97 // just do a createElement and skip the rest
98 ret = rsingleTag.exec( selector );
101 if ( jQuery.isPlainObject( context ) ) {
102 selector = [ document.createElement( ret[1] ) ];
103 jQuery.fn.attr.call( selector, context, true );
106 selector = [ doc.createElement( ret[1] ) ];
110 ret = buildFragment( [ match[1] ], [ doc ] );
111 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
114 return jQuery.merge( this, selector );
118 elem = document.getElementById( match[2] );
121 // Handle the case where IE and Opera return items
122 // by name instead of ID
123 if ( elem.id !== match[2] ) {
124 return rootjQuery.find( selector );
127 // Otherwise, we inject the element directly into the jQuery object
132 this.context = document;
133 this.selector = selector;
138 } else if ( !context && /^\w+$/.test( selector ) ) {
139 this.selector = selector;
140 this.context = document;
141 selector = document.getElementsByTagName( selector );
142 return jQuery.merge( this, selector );
144 // HANDLE: $(expr, $(...))
145 } else if ( !context || context.jquery ) {
146 return (context || rootjQuery).find( selector );
148 // HANDLE: $(expr, context)
149 // (which is just equivalent to: $(context).find(expr)
151 return jQuery( context ).find( selector );
154 // HANDLE: $(function)
155 // Shortcut for document ready
156 } else if ( jQuery.isFunction( selector ) ) {
157 return rootjQuery.ready( selector );
160 if (selector.selector !== undefined) {
161 this.selector = selector.selector;
162 this.context = selector.context;
165 return jQuery.makeArray( selector, this );
168 // Start with an empty selector
171 // The current version of jQuery being used
174 // The default length of a jQuery object is 0
177 // The number of elements contained in the matched element set
182 toArray: function() {
183 return slice.call( this, 0 );
186 // Get the Nth element in the matched element set OR
187 // Get the whole matched element set as a clean array
188 get: function( num ) {
191 // Return a 'clean' array
194 // Return just the object
195 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
198 // Take an array of elements and push it onto the stack
199 // (returning the new matched element set)
200 pushStack: function( elems, name, selector ) {
201 // Build a new jQuery matched element set
204 if ( jQuery.isArray( elems ) ) {
205 push.apply( ret, elems );
208 jQuery.merge( ret, elems );
211 // Add the old object onto the stack (as a reference)
212 ret.prevObject = this;
214 ret.context = this.context;
216 if ( name === "find" ) {
217 ret.selector = this.selector + (this.selector ? " " : "") + selector;
219 ret.selector = this.selector + "." + name + "(" + selector + ")";
222 // Return the newly-formed element set
226 // Execute a callback for every element in the matched set.
227 // (You can seed the arguments with an array of args, but this is
228 // only used internally.)
229 each: function( callback, args ) {
230 return jQuery.each( this, callback, args );
233 ready: function( fn ) {
234 // Attach the listeners
237 // If the DOM is already ready
238 if ( jQuery.isReady ) {
239 // Execute the function immediately
240 fn.call( document, jQuery );
242 // Otherwise, remember the function for later
243 } else if ( readyList ) {
244 // Add the function to the wait list
245 readyList.push( fn );
254 this.slice( i, +i + 1 );
262 return this.eq( -1 );
266 return this.pushStack( slice.apply( this, arguments ),
267 "slice", slice.call(arguments).join(",") );
270 map: function( callback ) {
271 return this.pushStack( jQuery.map(this, function( elem, i ) {
272 return callback.call( elem, i, elem );
277 return this.prevObject || jQuery(null);
280 // For internal use only.
281 // Behaves like an Array's method, not like a jQuery method.
287 // Give the init function the jQuery prototype for later instantiation
288 jQuery.fn.init.prototype = jQuery.fn;
290 jQuery.extend = jQuery.fn.extend = function() {
291 // copy reference to target object
292 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
294 // Handle a deep copy situation
295 if ( typeof target === "boolean" ) {
297 target = arguments[1] || {};
298 // skip the boolean and the target
302 // Handle case when target is a string or something (possible in deep copy)
303 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
307 // extend jQuery itself if only one argument is passed
308 if ( length === i ) {
313 for ( ; i < length; i++ ) {
314 // Only deal with non-null/undefined values
315 if ( (options = arguments[ i ]) != null ) {
316 // Extend the base object
317 for ( name in options ) {
318 src = target[ name ];
319 copy = options[ name ];
321 // Prevent never-ending loop
322 if ( target === copy ) {
326 // Recurse if we're merging object literal values or arrays
327 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
328 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
329 : jQuery.isArray(copy) ? [] : {};
331 // Never move original objects, clone them
332 target[ name ] = jQuery.extend( deep, clone, copy );
334 // Don't bring in undefined values
335 } else if ( copy !== undefined ) {
336 target[ name ] = copy;
342 // Return the modified object
347 noConflict: function( deep ) {
351 window.jQuery = _jQuery;
357 // Is the DOM ready to be used? Set to true once it occurs.
360 // Handle when the DOM is ready
362 // Make sure that the DOM is not already loaded
363 if ( !jQuery.isReady ) {
364 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
365 if ( !document.body ) {
366 return setTimeout( jQuery.ready, 13 );
369 // Remember that the DOM is ready
370 jQuery.isReady = true;
372 // If there are functions bound, to execute
374 // Execute all of them
376 while ( (fn = readyList[ i++ ]) ) {
377 fn.call( document, jQuery );
380 // Reset the list of functions
384 // Trigger any bound ready events
385 if ( jQuery.fn.triggerHandler ) {
386 jQuery( document ).triggerHandler( "ready" );
391 bindReady: function() {
398 // Catch cases where $(document).ready() is called after the
399 // browser event has already occurred.
400 if ( document.readyState === "complete" ) {
401 return jQuery.ready();
404 // Mozilla, Opera and webkit nightlies currently support this event
405 if ( document.addEventListener ) {
406 // Use the handy event callback
407 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
409 // A fallback to window.onload, that will always work
410 window.addEventListener( "load", jQuery.ready, false );
412 // If IE event model is used
413 } else if ( document.attachEvent ) {
414 // ensure firing before onload,
415 // maybe late but safe also for iframes
416 document.attachEvent("onreadystatechange", DOMContentLoaded);
418 // A fallback to window.onload, that will always work
419 window.attachEvent( "onload", jQuery.ready );
421 // If IE and not a frame
422 // continually check to see if the document is ready
423 var toplevel = false;
426 toplevel = window.frameElement == null;
429 if ( document.documentElement.doScroll && toplevel ) {
435 // See test/unit/core.js for details concerning isFunction.
436 // Since version 1.3, DOM methods and functions like alert
437 // aren't supported. They return false on IE (#2968).
438 isFunction: function( obj ) {
439 return toString.call(obj) === "[object Function]";
442 isArray: function( obj ) {
443 return toString.call(obj) === "[object Array]";
446 isPlainObject: function( obj ) {
447 // Must be an Object.
448 // Because of IE, we also have to check the presence of the constructor property.
449 // Make sure that DOM nodes and window objects don't pass through, as well
450 if ( !obj || toString.call(obj) !== "[object Object]" || obj.nodeType || obj.setInterval ) {
454 // Not own constructor property must be Object
455 if ( obj.constructor &&
456 !hasOwn.call(obj, "constructor") &&
457 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
461 // Own properties are enumerated firstly, so to speed up,
462 // if last one is own, then all properties are own.
465 for ( key in obj ) {}
467 return key === undefined || hasOwn.call( obj, key );
470 isEmptyObject: function( obj ) {
471 for ( var name in obj ) {
477 error: function( msg ) {
481 parseJSON: function( data ) {
482 if ( typeof data !== "string" || !data ) {
486 // Make sure leading/trailing whitespace is removed (IE can't handle it)
487 data = jQuery.trim( data );
489 // Make sure the incoming data is actual JSON
490 // Logic borrowed from http://json.org/json2.js
491 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
492 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
493 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
495 // Try to use the native JSON parser first
496 return window.JSON && window.JSON.parse ?
497 window.JSON.parse( data ) :
498 (new Function("return " + data))();
501 jQuery.error( "Invalid JSON: " + data );
507 // Evalulates a script in a global context
508 globalEval: function( data ) {
509 if ( data && rnotwhite.test(data) ) {
510 // Inspired by code by Andrea Giammarchi
511 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
512 var head = document.getElementsByTagName("head")[0] || document.documentElement,
513 script = document.createElement("script");
515 script.type = "text/javascript";
517 if ( jQuery.support.scriptEval ) {
518 script.appendChild( document.createTextNode( data ) );
523 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
524 // This arises when a base node is used (#2709).
525 head.insertBefore( script, head.firstChild );
526 head.removeChild( script );
530 nodeName: function( elem, name ) {
531 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
534 // args is for internal usage only
535 each: function( object, callback, args ) {
537 length = object.length,
538 isObj = length === undefined || jQuery.isFunction(object);
542 for ( name in object ) {
543 if ( callback.apply( object[ name ], args ) === false ) {
548 for ( ; i < length; ) {
549 if ( callback.apply( object[ i++ ], args ) === false ) {
555 // A special, fast, case for the most common use of each
558 for ( name in object ) {
559 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
564 for ( var value = object[0];
565 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
572 // Use native String.trim function wherever possible
575 return text == null ?
580 // Otherwise use our own trimming functionality
582 return text == null ?
584 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
587 // results is for internal usage only
588 makeArray: function( array, results ) {
589 var ret = results || [];
591 if ( array != null ) {
592 // The window, strings (and functions) also have 'length'
593 // The extra typeof function check is to prevent crashes
594 // in Safari 2 (See: #3039)
595 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
596 push.call( ret, array );
598 jQuery.merge( ret, array );
605 inArray: function( elem, array ) {
606 if ( array.indexOf ) {
607 return array.indexOf( elem );
610 for ( var i = 0, length = array.length; i < length; i++ ) {
611 if ( array[ i ] === elem ) {
619 merge: function( first, second ) {
620 var i = first.length, j = 0;
622 if ( typeof second.length === "number" ) {
623 for ( var l = second.length; j < l; j++ ) {
624 first[ i++ ] = second[ j ];
628 while ( second[j] !== undefined ) {
629 first[ i++ ] = second[ j++ ];
638 grep: function( elems, callback, inv ) {
639 var ret = [], retVal;
642 // Go through the array, only saving the items
643 // that pass the validator function
644 for ( var i = 0, length = elems.length; i < length; i++ ) {
645 retVal = !!callback( elems[ i ], i );
646 if ( inv !== retVal ) {
647 ret.push( elems[ i ] );
654 // arg is for internal usage only
655 map: function( elems, callback, arg ) {
658 // Go through the array, translating each of the items to their
659 // new value (or values).
660 for ( var i = 0, length = elems.length; i < length; i++ ) {
661 value = callback( elems[ i ], i, arg );
663 if ( value != null ) {
664 ret[ ret.length ] = value;
668 return ret.concat.apply( [], ret );
671 // A global GUID counter for objects
674 proxy: function( fn, proxy, thisObject ) {
675 if ( arguments.length === 2 ) {
676 if ( typeof proxy === "string" ) {
678 fn = thisObject[ proxy ];
681 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
687 if ( !proxy && fn ) {
689 return fn.apply( thisObject || this, arguments );
693 // Set the guid of unique handler to the same of original handler, so it can be removed
695 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
698 // So proxy can be declared as an argument
702 // Use of jQuery.browser is frowned upon.
703 // More details: http://docs.jquery.com/Utilities/jQuery.browser
704 uaMatch: function( ua ) {
705 ua = ua.toLowerCase();
707 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
708 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
709 /(msie) ([\w.]+)/.exec( ua ) ||
710 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
713 return { browser: match[1] || "", version: match[2] || "0" };
719 browserMatch = jQuery.uaMatch( userAgent );
720 if ( browserMatch.browser ) {
721 jQuery.browser[ browserMatch.browser ] = true;
722 jQuery.browser.version = browserMatch.version;
725 // Deprecated, use jQuery.browser.webkit instead
726 if ( jQuery.browser.webkit ) {
727 jQuery.browser.safari = true;
731 jQuery.inArray = function( elem, array ) {
732 return indexOf.call( array, elem );
736 // Verify that \s matches non-breaking spaces
737 // (IE fails on this test)
738 if ( !/\s/.test( "\xA0" ) ) {
739 trimLeft = /^[\s\xA0]+/;
740 trimRight = /[\s\xA0]+$/;
743 // All jQuery objects should point back to these
744 rootjQuery = jQuery(document);
746 // Cleanup functions for the document ready method
747 if ( document.addEventListener ) {
748 DOMContentLoaded = function() {
749 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
753 } else if ( document.attachEvent ) {
754 DOMContentLoaded = function() {
755 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
756 if ( document.readyState === "complete" ) {
757 document.detachEvent( "onreadystatechange", DOMContentLoaded );
763 // The DOM ready check for Internet Explorer
764 function doScrollCheck() {
765 if ( jQuery.isReady ) {
770 // If IE is used, use the trick by Diego Perini
771 // http://javascript.nwbox.com/IEContentLoaded/
772 document.documentElement.doScroll("left");
774 setTimeout( doScrollCheck, 1 );
778 // and execute any waiting functions
782 function evalScript( i, elem ) {
790 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
793 if ( elem.parentNode ) {
794 elem.parentNode.removeChild( elem );
798 // Mutifunctional method to get and set values to a collection
799 // The value/s can be optionally by executed if its a function
800 function access( elems, key, value, exec, fn, pass ) {
801 var length = elems.length;
803 // Setting many attributes
804 if ( typeof key === "object" ) {
805 for ( var k in key ) {
806 access( elems, k, key[k], exec, fn, value );
811 // Setting one attribute
812 if ( value !== undefined ) {
813 // Optionally, function values get executed if exec is true
814 exec = !pass && exec && jQuery.isFunction(value);
816 for ( var i = 0; i < length; i++ ) {
817 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
823 // Getting an attribute
824 return length ? fn( elems[0], key ) : undefined;
828 return (new Date()).getTime();