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
30 rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g,
32 // Match a standalone tag
33 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
35 // Keep a UserAgent string for use with jQuery.browser
36 userAgent = navigator.userAgent,
38 // For matching the engine and version of the browser
41 // Has the ready events already been bound?
44 // The functions to execute on DOM ready
47 // The ready event handler
50 // Save a reference to some core methods
51 toString = Object.prototype.toString,
52 hasOwnProperty = Object.prototype.hasOwnProperty,
53 push = Array.prototype.push,
54 slice = Array.prototype.slice,
55 indexOf = Array.prototype.indexOf;
57 jQuery.fn = jQuery.prototype = {
58 init: function( selector, context ) {
59 var match, elem, ret, doc;
61 // Handle $(""), $(null), or $(undefined)
66 // Handle $(DOMElement)
67 if ( selector.nodeType ) {
68 this.context = this[0] = selector;
73 // The body element only exists once, optimize finding it
74 if ( selector === "body" && !context ) {
75 this.context = document;
76 this[0] = document.body;
77 this.selector = "body";
82 // Handle HTML strings
83 if ( typeof selector === "string" ) {
84 // Are we dealing with HTML string or an ID?
85 match = quickExpr.exec( selector );
87 // Verify a match, and that no context was specified for #id
88 if ( match && (match[1] || !context) ) {
90 // HANDLE: $(html) -> $(array)
92 doc = (context ? context.ownerDocument || context : document);
94 // If a single string is passed in and it's a single tag
95 // just do a createElement and skip the rest
96 ret = rsingleTag.exec( selector );
99 if ( jQuery.isPlainObject( context ) ) {
100 selector = [ document.createElement( ret[1] ) ];
101 jQuery.fn.attr.call( selector, context, true );
104 selector = [ doc.createElement( ret[1] ) ];
108 ret = buildFragment( [ match[1] ], [ doc ] );
109 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
112 return jQuery.merge( this, selector );
116 elem = document.getElementById( match[2] );
119 // Handle the case where IE and Opera return items
120 // by name instead of ID
121 if ( elem.id !== match[2] ) {
122 return rootjQuery.find( selector );
125 // Otherwise, we inject the element directly into the jQuery object
130 this.context = document;
131 this.selector = selector;
136 } else if ( !context && /^\w+$/.test( selector ) ) {
137 this.selector = selector;
138 this.context = document;
139 selector = document.getElementsByTagName( selector );
140 return jQuery.merge( this, selector );
142 // HANDLE: $(expr, $(...))
143 } else if ( !context || context.jquery ) {
144 return (context || rootjQuery).find( selector );
146 // HANDLE: $(expr, context)
147 // (which is just equivalent to: $(context).find(expr)
149 return jQuery( context ).find( selector );
152 // HANDLE: $(function)
153 // Shortcut for document ready
154 } else if ( jQuery.isFunction( selector ) ) {
155 return rootjQuery.ready( selector );
158 if (selector.selector !== undefined) {
159 this.selector = selector.selector;
160 this.context = selector.context;
163 return jQuery.makeArray( selector, this );
166 // Start with an empty selector
169 // The current version of jQuery being used
172 // The default length of a jQuery object is 0
175 // The number of elements contained in the matched element set
180 toArray: function() {
181 return slice.call( this, 0 );
184 // Get the Nth element in the matched element set OR
185 // Get the whole matched element set as a clean array
186 get: function( num ) {
189 // Return a 'clean' array
192 // Return just the object
193 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
196 // Take an array of elements and push it onto the stack
197 // (returning the new matched element set)
198 pushStack: function( elems, name, selector ) {
199 // Build a new jQuery matched element set
200 var ret = jQuery( elems || null );
202 // Add the old object onto the stack (as a reference)
203 ret.prevObject = this;
205 ret.context = this.context;
207 if ( name === "find" ) {
208 ret.selector = this.selector + (this.selector ? " " : "") + selector;
210 ret.selector = this.selector + "." + name + "(" + selector + ")";
213 // Return the newly-formed element set
217 // Force the current matched set of elements to become
218 // the specified array of elements (destroying the stack in the process)
219 // You should use pushStack() in order to do this, but maintain the stack
220 setArray: function( elems ) {
221 // Resetting the length to 0, then using the native Array push
222 // is a super-fast way to populate an object with array-like properties
224 push.apply( this, elems );
229 // Execute a callback for every element in the matched set.
230 // (You can seed the arguments with an array of args, but this is
231 // only used internally.)
232 each: function( callback, args ) {
233 return jQuery.each( this, callback, args );
236 ready: function( fn ) {
237 // Attach the listeners
240 // If the DOM is already ready
241 if ( jQuery.isReady ) {
242 // Execute the function immediately
243 fn.call( document, jQuery );
245 // Otherwise, remember the function for later
246 } else if ( readyList ) {
247 // Add the function to the wait list
248 readyList.push( fn );
257 this.slice( i, +i + 1 );
265 return this.eq( -1 );
269 return this.pushStack( slice.apply( this, arguments ),
270 "slice", slice.call(arguments).join(",") );
273 map: function( callback ) {
274 return this.pushStack( jQuery.map(this, function( elem, i ) {
275 return callback.call( elem, i, elem );
280 return this.prevObject || jQuery(null);
283 // For internal use only.
284 // Behaves like an Array's method, not like a jQuery method.
290 // Give the init function the jQuery prototype for later instantiation
291 jQuery.fn.init.prototype = jQuery.fn;
293 jQuery.extend = jQuery.fn.extend = function() {
294 // copy reference to target object
295 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
297 // Handle a deep copy situation
298 if ( typeof target === "boolean" ) {
300 target = arguments[1] || {};
301 // skip the boolean and the target
305 // Handle case when target is a string or something (possible in deep copy)
306 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
310 // extend jQuery itself if only one argument is passed
311 if ( length === i ) {
316 for ( ; i < length; i++ ) {
317 // Only deal with non-null/undefined values
318 if ( (options = arguments[ i ]) != null ) {
319 // Extend the base object
320 for ( name in options ) {
321 src = target[ name ];
322 copy = options[ name ];
324 // Prevent never-ending loop
325 if ( target === copy ) {
329 // Recurse if we're merging object literal values or arrays
330 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
331 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
332 : jQuery.isArray(copy) ? [] : {};
334 // Never move original objects, clone them
335 target[ name ] = jQuery.extend( deep, clone, copy );
337 // Don't bring in undefined values
338 } else if ( copy !== undefined ) {
339 target[ name ] = copy;
345 // Return the modified object
350 noConflict: function( deep ) {
354 window.jQuery = _jQuery;
360 // Is the DOM ready to be used? Set to true once it occurs.
363 // Handle when the DOM is ready
365 // Make sure that the DOM is not already loaded
366 if ( !jQuery.isReady ) {
367 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
368 if ( !document.body ) {
369 return setTimeout( jQuery.ready, 13 );
372 // Remember that the DOM is ready
373 jQuery.isReady = true;
375 // If there are functions bound, to execute
377 // Execute all of them
379 while ( (fn = readyList[ i++ ]) ) {
380 fn.call( document, jQuery );
383 // Reset the list of functions
387 // Trigger any bound ready events
388 if ( jQuery.fn.triggerHandler ) {
389 jQuery( document ).triggerHandler( "ready" );
394 bindReady: function() {
401 // Catch cases where $(document).ready() is called after the
402 // browser event has already occurred.
403 if ( document.readyState === "complete" ) {
404 return jQuery.ready();
407 // Mozilla, Opera and webkit nightlies currently support this event
408 if ( document.addEventListener ) {
409 // Use the handy event callback
410 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
412 // A fallback to window.onload, that will always work
413 window.addEventListener( "load", jQuery.ready, false );
415 // If IE event model is used
416 } else if ( document.attachEvent ) {
417 // ensure firing before onload,
418 // maybe late but safe also for iframes
419 document.attachEvent("onreadystatechange", DOMContentLoaded);
421 // A fallback to window.onload, that will always work
422 window.attachEvent( "onload", jQuery.ready );
424 // If IE and not a frame
425 // continually check to see if the document is ready
426 var toplevel = false;
429 toplevel = window.frameElement == null;
432 if ( document.documentElement.doScroll && toplevel ) {
438 // See test/unit/core.js for details concerning isFunction.
439 // Since version 1.3, DOM methods and functions like alert
440 // aren't supported. They return false on IE (#2968).
441 isFunction: function( obj ) {
442 return toString.call(obj) === "[object Function]";
445 isArray: function( obj ) {
446 return toString.call(obj) === "[object Array]";
449 isPlainObject: function( obj ) {
450 // Must be an Object.
451 // Because of IE, we also have to check the presence of the constructor property.
452 // Make sure that DOM nodes and window objects don't pass through, as well
453 if ( !obj || toString.call(obj) !== "[object Object]" || obj.nodeType || obj.setInterval ) {
457 // Not own constructor property must be Object
459 && !hasOwnProperty.call(obj, "constructor")
460 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
464 // Own properties are enumerated firstly, so to speed up,
465 // if last one is own, then all properties are own.
468 for ( key in obj ) {}
470 return key === undefined || hasOwnProperty.call( obj, key );
473 isEmptyObject: function( obj ) {
474 for ( var name in obj ) {
480 error: function( msg ) {
484 parseJSON: function( data ) {
485 if ( typeof data !== "string" || !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 trim: function( text ) {
573 return (text || "").replace( rtrim, "" );
576 // results is for internal usage only
577 makeArray: function( array, results ) {
578 var ret = results || [];
580 if ( array != null ) {
581 // The window, strings (and functions) also have 'length'
582 // The extra typeof function check is to prevent crashes
583 // in Safari 2 (See: #3039)
584 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
585 push.call( ret, array );
587 jQuery.merge( ret, array );
594 inArray: function( elem, array ) {
595 if ( array.indexOf ) {
596 return array.indexOf( elem );
599 for ( var i = 0, length = array.length; i < length; i++ ) {
600 if ( array[ i ] === elem ) {
608 merge: function( first, second ) {
609 var i = first.length, j = 0;
611 if ( typeof second.length === "number" ) {
612 for ( var l = second.length; j < l; j++ ) {
613 first[ i++ ] = second[ j ];
617 while ( second[j] !== undefined ) {
618 first[ i++ ] = second[ j++ ];
627 grep: function( elems, callback, inv ) {
630 // Go through the array, only saving the items
631 // that pass the validator function
632 for ( var i = 0, length = elems.length; i < length; i++ ) {
633 if ( !inv !== !callback( elems[ i ], i ) ) {
634 ret.push( elems[ i ] );
641 // arg is for internal usage only
642 map: function( elems, callback, arg ) {
645 // Go through the array, translating each of the items to their
646 // new value (or values).
647 for ( var i = 0, length = elems.length; i < length; i++ ) {
648 value = callback( elems[ i ], i, arg );
650 if ( value != null ) {
651 ret[ ret.length ] = value;
655 return ret.concat.apply( [], ret );
658 // A global GUID counter for objects
661 proxy: function( fn, proxy, thisObject ) {
662 if ( arguments.length === 2 ) {
663 if ( typeof proxy === "string" ) {
665 fn = thisObject[ proxy ];
668 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
674 if ( !proxy && fn ) {
676 return fn.apply( thisObject || this, arguments );
680 // Set the guid of unique handler to the same of original handler, so it can be removed
682 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
685 // So proxy can be declared as an argument
689 // Use of jQuery.browser is frowned upon.
690 // More details: http://docs.jquery.com/Utilities/jQuery.browser
691 uaMatch: function( ua ) {
692 ua = ua.toLowerCase();
694 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
695 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
696 /(msie) ([\w.]+)/.exec( ua ) ||
697 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
700 return { browser: match[1] || "", version: match[2] || "0" };
706 browserMatch = jQuery.uaMatch( userAgent );
707 if ( browserMatch.browser ) {
708 jQuery.browser[ browserMatch.browser ] = true;
709 jQuery.browser.version = browserMatch.version;
712 // Deprecated, use jQuery.browser.webkit instead
713 if ( jQuery.browser.webkit ) {
714 jQuery.browser.safari = true;
718 jQuery.inArray = function( elem, array ) {
719 return indexOf.call( array, elem );
723 // All jQuery objects should point back to these
724 rootjQuery = jQuery(document);
726 // Cleanup functions for the document ready method
727 if ( document.addEventListener ) {
728 DOMContentLoaded = function() {
729 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
733 } else if ( document.attachEvent ) {
734 DOMContentLoaded = function() {
735 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
736 if ( document.readyState === "complete" ) {
737 document.detachEvent( "onreadystatechange", DOMContentLoaded );
743 // The DOM ready check for Internet Explorer
744 function doScrollCheck() {
745 if ( jQuery.isReady ) {
750 // If IE is used, use the trick by Diego Perini
751 // http://javascript.nwbox.com/IEContentLoaded/
752 document.documentElement.doScroll("left");
754 setTimeout( doScrollCheck, 1 );
758 // and execute any waiting functions
762 function evalScript( i, elem ) {
770 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
773 if ( elem.parentNode ) {
774 elem.parentNode.removeChild( elem );
778 // Mutifunctional method to get and set values to a collection
779 // The value/s can be optionally by executed if its a function
780 function access( elems, key, value, exec, fn, pass ) {
781 var length = elems.length;
783 // Setting many attributes
784 if ( typeof key === "object" ) {
785 for ( var k in key ) {
786 access( elems, k, key[k], exec, fn, value );
791 // Setting one attribute
792 if ( value !== undefined ) {
793 // Optionally, function values get executed if exec is true
794 exec = !pass && exec && jQuery.isFunction(value);
796 for ( var i = 0; i < length; i++ ) {
797 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
803 // Getting an attribute
804 return length ? fn( elems[0], key ) : null;
808 return (new Date).getTime();