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 // Handle HTML strings
74 if ( typeof selector === "string" ) {
75 // Are we dealing with HTML string or an ID?
76 match = quickExpr.exec( selector );
78 // Verify a match, and that no context was specified for #id
79 if ( match && (match[1] || !context) ) {
81 // HANDLE: $(html) -> $(array)
83 doc = (context ? context.ownerDocument || context : document);
85 // If a single string is passed in and it's a single tag
86 // just do a createElement and skip the rest
87 ret = rsingleTag.exec( selector );
90 if ( jQuery.isPlainObject( context ) ) {
91 selector = [ document.createElement( ret[1] ) ];
92 jQuery.fn.attr.call( selector, context, true );
95 selector = [ doc.createElement( ret[1] ) ];
99 ret = buildFragment( [ match[1] ], [ doc ] );
100 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
105 elem = document.getElementById( match[2] );
108 // Handle the case where IE and Opera return items
109 // by name instead of ID
110 if ( elem.id !== match[2] ) {
111 return rootjQuery.find( selector );
114 // Otherwise, we inject the element directly into the jQuery object
119 this.context = document;
120 this.selector = selector;
125 } else if ( !context && /^\w+$/.test( selector ) ) {
126 this.selector = selector;
127 this.context = document;
128 selector = document.getElementsByTagName( selector );
130 // HANDLE: $(expr, $(...))
131 } else if ( !context || context.jquery ) {
132 return (context || rootjQuery).find( selector );
134 // HANDLE: $(expr, context)
135 // (which is just equivalent to: $(context).find(expr)
137 return jQuery( context ).find( selector );
140 // HANDLE: $(function)
141 // Shortcut for document ready
142 } else if ( jQuery.isFunction( selector ) ) {
143 return rootjQuery.ready( selector );
146 if (selector.selector !== undefined) {
147 this.selector = selector.selector;
148 this.context = selector.context;
151 return jQuery.isArray( selector ) ?
152 this.setArray( selector ) :
153 jQuery.makeArray( selector, this );
156 // Start with an empty selector
159 // The current version of jQuery being used
162 // The default length of a jQuery object is 0
165 // The number of elements contained in the matched element set
170 toArray: function() {
171 return slice.call( this, 0 );
174 // Get the Nth element in the matched element set OR
175 // Get the whole matched element set as a clean array
176 get: function( num ) {
179 // Return a 'clean' array
182 // Return just the object
183 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
186 // Take an array of elements and push it onto the stack
187 // (returning the new matched element set)
188 pushStack: function( elems, name, selector ) {
189 // Build a new jQuery matched element set
190 var ret = jQuery( elems || null );
192 // Add the old object onto the stack (as a reference)
193 ret.prevObject = this;
195 ret.context = this.context;
197 if ( name === "find" ) {
198 ret.selector = this.selector + (this.selector ? " " : "") + selector;
200 ret.selector = this.selector + "." + name + "(" + selector + ")";
203 // Return the newly-formed element set
207 // Force the current matched set of elements to become
208 // the specified array of elements (destroying the stack in the process)
209 // You should use pushStack() in order to do this, but maintain the stack
210 setArray: function( elems ) {
211 // Resetting the length to 0, then using the native Array push
212 // is a super-fast way to populate an object with array-like properties
214 push.apply( this, elems );
219 // Execute a callback for every element in the matched set.
220 // (You can seed the arguments with an array of args, but this is
221 // only used internally.)
222 each: function( callback, args ) {
223 return jQuery.each( this, callback, args );
226 ready: function( fn ) {
227 // Attach the listeners
230 // If the DOM is already ready
231 if ( jQuery.isReady ) {
232 // Execute the function immediately
233 fn.call( document, jQuery );
235 // Otherwise, remember the function for later
236 } else if ( readyList ) {
237 // Add the function to the wait list
238 readyList.push( fn );
247 this.slice( i, +i + 1 );
255 return this.eq( -1 );
259 return this.pushStack( slice.apply( this, arguments ),
260 "slice", slice.call(arguments).join(",") );
263 map: function( callback ) {
264 return this.pushStack( jQuery.map(this, function( elem, i ) {
265 return callback.call( elem, i, elem );
270 return this.prevObject || jQuery(null);
273 // For internal use only.
274 // Behaves like an Array's method, not like a jQuery method.
280 // Give the init function the jQuery prototype for later instantiation
281 jQuery.fn.init.prototype = jQuery.fn;
283 jQuery.extend = jQuery.fn.extend = function() {
284 // copy reference to target object
285 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
287 // Handle a deep copy situation
288 if ( typeof target === "boolean" ) {
290 target = arguments[1] || {};
291 // skip the boolean and the target
295 // Handle case when target is a string or something (possible in deep copy)
296 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
300 // extend jQuery itself if only one argument is passed
301 if ( length === i ) {
306 for ( ; i < length; i++ ) {
307 // Only deal with non-null/undefined values
308 if ( (options = arguments[ i ]) != null ) {
309 // Extend the base object
310 for ( name in options ) {
311 src = target[ name ];
312 copy = options[ name ];
314 // Prevent never-ending loop
315 if ( target === copy ) {
319 // Recurse if we're merging object literal values or arrays
320 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
321 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
322 : jQuery.isArray(copy) ? [] : {};
324 // Never move original objects, clone them
325 target[ name ] = jQuery.extend( deep, clone, copy );
327 // Don't bring in undefined values
328 } else if ( copy !== undefined ) {
329 target[ name ] = copy;
335 // Return the modified object
340 noConflict: function( deep ) {
344 window.jQuery = _jQuery;
350 // Is the DOM ready to be used? Set to true once it occurs.
353 // Handle when the DOM is ready
355 // Make sure that the DOM is not already loaded
356 if ( !jQuery.isReady ) {
357 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
358 if ( !document.body ) {
359 return setTimeout( jQuery.ready, 13 );
362 // Remember that the DOM is ready
363 jQuery.isReady = true;
365 // If there are functions bound, to execute
367 // Execute all of them
369 while ( (fn = readyList[ i++ ]) ) {
370 fn.call( document, jQuery );
373 // Reset the list of functions
377 // Trigger any bound ready events
378 if ( jQuery.fn.triggerHandler ) {
379 jQuery( document ).triggerHandler( "ready" );
384 bindReady: function() {
391 // Catch cases where $(document).ready() is called after the
392 // browser event has already occurred.
393 if ( document.readyState === "complete" ) {
394 return jQuery.ready();
397 // Mozilla, Opera and webkit nightlies currently support this event
398 if ( document.addEventListener ) {
399 // Use the handy event callback
400 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
402 // A fallback to window.onload, that will always work
403 window.addEventListener( "load", jQuery.ready, false );
405 // If IE event model is used
406 } else if ( document.attachEvent ) {
407 // ensure firing before onload,
408 // maybe late but safe also for iframes
409 document.attachEvent("onreadystatechange", DOMContentLoaded);
411 // A fallback to window.onload, that will always work
412 window.attachEvent( "onload", jQuery.ready );
414 // If IE and not a frame
415 // continually check to see if the document is ready
416 var toplevel = false;
419 toplevel = window.frameElement == null;
422 if ( document.documentElement.doScroll && toplevel ) {
428 // See test/unit/core.js for details concerning isFunction.
429 // Since version 1.3, DOM methods and functions like alert
430 // aren't supported. They return false on IE (#2968).
431 isFunction: function( obj ) {
432 return toString.call(obj) === "[object Function]";
435 isArray: function( obj ) {
436 return toString.call(obj) === "[object Array]";
439 isPlainObject: function( obj ) {
440 // Must be an Object.
441 // Because of IE, we also have to check the presence of the constructor property.
442 // Make sure that DOM nodes and window objects don't pass through, as well
443 if ( !obj || toString.call(obj) !== "[object Object]" || obj.nodeType || obj.setInterval ) {
447 // Not own constructor property must be Object
449 && !hasOwnProperty.call(obj, "constructor")
450 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
454 // Own properties are enumerated firstly, so to speed up,
455 // if last one is own, then all properties are own.
458 for ( key in obj ) {}
460 return key === undefined || hasOwnProperty.call( obj, key );
463 isEmptyObject: function( obj ) {
464 for ( var name in obj ) {
470 error: function( msg ) {
474 parseJSON: function( data ) {
475 if ( typeof data !== "string" || !data ) {
479 // Make sure the incoming data is actual JSON
480 // Logic borrowed from http://json.org/json2.js
481 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
482 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
483 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
485 // Try to use the native JSON parser first
486 return window.JSON && window.JSON.parse ?
487 window.JSON.parse( data ) :
488 (new Function("return " + data))();
491 jQuery.error( "Invalid JSON: " + data );
497 // Evalulates a script in a global context
498 globalEval: function( data ) {
499 if ( data && rnotwhite.test(data) ) {
500 // Inspired by code by Andrea Giammarchi
501 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
502 var head = document.getElementsByTagName("head")[0] || document.documentElement,
503 script = document.createElement("script");
505 script.type = "text/javascript";
507 if ( jQuery.support.scriptEval ) {
508 script.appendChild( document.createTextNode( data ) );
513 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
514 // This arises when a base node is used (#2709).
515 head.insertBefore( script, head.firstChild );
516 head.removeChild( script );
520 nodeName: function( elem, name ) {
521 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
524 // args is for internal usage only
525 each: function( object, callback, args ) {
527 length = object.length,
528 isObj = length === undefined || jQuery.isFunction(object);
532 for ( name in object ) {
533 if ( callback.apply( object[ name ], args ) === false ) {
538 for ( ; i < length; ) {
539 if ( callback.apply( object[ i++ ], args ) === false ) {
545 // A special, fast, case for the most common use of each
548 for ( name in object ) {
549 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
554 for ( var value = object[0];
555 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
562 trim: function( text ) {
563 return (text || "").replace( rtrim, "" );
566 // results is for internal usage only
567 makeArray: function( array, results ) {
568 var ret = results || [];
570 if ( array != null ) {
571 // The window, strings (and functions) also have 'length'
572 // The extra typeof function check is to prevent crashes
573 // in Safari 2 (See: #3039)
574 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
575 push.call( ret, array );
577 jQuery.merge( ret, array );
584 inArray: function( elem, array ) {
585 if ( array.indexOf ) {
586 return array.indexOf( elem );
589 for ( var i = 0, length = array.length; i < length; i++ ) {
590 if ( array[ i ] === elem ) {
598 merge: function( first, second ) {
599 var i = first.length, j = 0;
601 if ( typeof second.length === "number" ) {
602 for ( var l = second.length; j < l; j++ ) {
603 first[ i++ ] = second[ j ];
606 while ( second[j] !== undefined ) {
607 first[ i++ ] = second[ j++ ];
616 grep: function( elems, callback, inv ) {
619 // Go through the array, only saving the items
620 // that pass the validator function
621 for ( var i = 0, length = elems.length; i < length; i++ ) {
622 if ( !inv !== !callback( elems[ i ], i ) ) {
623 ret.push( elems[ i ] );
630 // arg is for internal usage only
631 map: function( elems, callback, arg ) {
634 // Go through the array, translating each of the items to their
635 // new value (or values).
636 for ( var i = 0, length = elems.length; i < length; i++ ) {
637 value = callback( elems[ i ], i, arg );
639 if ( value != null ) {
640 ret[ ret.length ] = value;
644 return ret.concat.apply( [], ret );
647 // A global GUID counter for objects
650 proxy: function( fn, proxy, thisObject ) {
651 if ( arguments.length === 2 ) {
652 if ( typeof proxy === "string" ) {
654 fn = thisObject[ proxy ];
657 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
663 if ( !proxy && fn ) {
665 return fn.apply( thisObject || this, arguments );
669 // Set the guid of unique handler to the same of original handler, so it can be removed
671 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
674 // So proxy can be declared as an argument
678 // Use of jQuery.browser is frowned upon.
679 // More details: http://docs.jquery.com/Utilities/jQuery.browser
680 uaMatch: function( ua ) {
681 ua = ua.toLowerCase();
683 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
684 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
685 /(msie) ([\w.]+)/.exec( ua ) ||
686 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
689 return { browser: match[1] || "", version: match[2] || "0" };
695 browserMatch = jQuery.uaMatch( userAgent );
696 if ( browserMatch.browser ) {
697 jQuery.browser[ browserMatch.browser ] = true;
698 jQuery.browser.version = browserMatch.version;
701 // Deprecated, use jQuery.browser.webkit instead
702 if ( jQuery.browser.webkit ) {
703 jQuery.browser.safari = true;
707 jQuery.inArray = function( elem, array ) {
708 return indexOf.call( array, elem );
712 // All jQuery objects should point back to these
713 rootjQuery = jQuery(document);
715 // Cleanup functions for the document ready method
716 if ( document.addEventListener ) {
717 DOMContentLoaded = function() {
718 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
722 } else if ( document.attachEvent ) {
723 DOMContentLoaded = function() {
724 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
725 if ( document.readyState === "complete" ) {
726 document.detachEvent( "onreadystatechange", DOMContentLoaded );
732 // The DOM ready check for Internet Explorer
733 function doScrollCheck() {
734 if ( jQuery.isReady ) {
739 // If IE is used, use the trick by Diego Perini
740 // http://javascript.nwbox.com/IEContentLoaded/
741 document.documentElement.doScroll("left");
743 setTimeout( doScrollCheck, 1 );
747 // and execute any waiting functions
751 function evalScript( i, elem ) {
759 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
762 if ( elem.parentNode ) {
763 elem.parentNode.removeChild( elem );
767 // Mutifunctional method to get and set values to a collection
768 // The value/s can be optionally by executed if its a function
769 function access( elems, key, value, exec, fn, pass ) {
770 var length = elems.length;
772 // Setting many attributes
773 if ( typeof key === "object" ) {
774 for ( var k in key ) {
775 access( elems, k, key[k], exec, fn, value );
780 // Setting one attribute
781 if ( value !== undefined ) {
782 // Optionally, function values get executed if exec is true
783 exec = !pass && exec && jQuery.isFunction(value);
785 for ( var i = 0; i < length; i++ ) {
786 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
792 // Getting an attribute
793 return length ? fn( elems[0], key ) : null;
797 return (new Date).getTime();