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 indexOf = Array.prototype.indexOf;
58 jQuery.fn = jQuery.prototype = {
59 init: function( selector, context ) {
60 var match, elem, ret, doc;
62 // Handle $(""), $(null), or $(undefined)
67 // Handle $(DOMElement)
68 if ( selector.nodeType ) {
69 this.context = this[0] = selector;
74 // The body element only exists once, optimize finding it
75 if ( selector === "body" && !context ) {
76 this.context = document;
77 this[0] = document.body;
78 this.selector = "body";
83 // Handle HTML strings
84 if ( typeof selector === "string" ) {
85 // Are we dealing with HTML string or an ID?
86 match = quickExpr.exec( selector );
88 // Verify a match, and that no context was specified for #id
89 if ( match && (match[1] || !context) ) {
91 // HANDLE: $(html) -> $(array)
93 doc = (context ? context.ownerDocument || context : document);
95 // If a single string is passed in and it's a single tag
96 // just do a createElement and skip the rest
97 ret = rsingleTag.exec( selector );
100 if ( jQuery.isPlainObject( context ) ) {
101 selector = [ document.createElement( ret[1] ) ];
102 jQuery.fn.attr.call( selector, context, true );
105 selector = [ doc.createElement( ret[1] ) ];
109 ret = buildFragment( [ match[1] ], [ doc ] );
110 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
113 return jQuery.merge( this, selector );
117 elem = document.getElementById( match[2] );
120 // Handle the case where IE and Opera return items
121 // by name instead of ID
122 if ( elem.id !== match[2] ) {
123 return rootjQuery.find( selector );
126 // Otherwise, we inject the element directly into the jQuery object
131 this.context = document;
132 this.selector = selector;
137 } else if ( !context && /^\w+$/.test( selector ) ) {
138 this.selector = selector;
139 this.context = document;
140 selector = document.getElementsByTagName( selector );
141 return jQuery.merge( this, selector );
143 // HANDLE: $(expr, $(...))
144 } else if ( !context || context.jquery ) {
145 return (context || rootjQuery).find( selector );
147 // HANDLE: $(expr, context)
148 // (which is just equivalent to: $(context).find(expr)
150 return jQuery( context ).find( selector );
153 // HANDLE: $(function)
154 // Shortcut for document ready
155 } else if ( jQuery.isFunction( selector ) ) {
156 return rootjQuery.ready( selector );
159 if (selector.selector !== undefined) {
160 this.selector = selector.selector;
161 this.context = selector.context;
164 return jQuery.makeArray( selector, this );
167 // Start with an empty selector
170 // The current version of jQuery being used
173 // The default length of a jQuery object is 0
176 // The number of elements contained in the matched element set
181 toArray: function() {
182 return slice.call( this, 0 );
185 // Get the Nth element in the matched element set OR
186 // Get the whole matched element set as a clean array
187 get: function( num ) {
190 // Return a 'clean' array
193 // Return just the object
194 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
197 // Take an array of elements and push it onto the stack
198 // (returning the new matched element set)
199 pushStack: function( elems, name, selector ) {
200 // Build a new jQuery matched element set
203 if ( jQuery.isArray( elems ) ) {
204 push.apply( ret, elems );
207 jQuery.merge( ret, elems );
210 // Add the old object onto the stack (as a reference)
211 ret.prevObject = this;
213 ret.context = this.context;
215 if ( name === "find" ) {
216 ret.selector = this.selector + (this.selector ? " " : "") + selector;
218 ret.selector = this.selector + "." + name + "(" + selector + ")";
221 // Return the newly-formed element set
225 // Execute a callback for every element in the matched set.
226 // (You can seed the arguments with an array of args, but this is
227 // only used internally.)
228 each: function( callback, args ) {
229 return jQuery.each( this, callback, args );
232 ready: function( fn ) {
233 // Attach the listeners
236 // If the DOM is already ready
237 if ( jQuery.isReady ) {
238 // Execute the function immediately
239 fn.call( document, jQuery );
241 // Otherwise, remember the function for later
242 } else if ( readyList ) {
243 // Add the function to the wait list
244 readyList.push( fn );
253 this.slice( i, +i + 1 );
261 return this.eq( -1 );
265 return this.pushStack( slice.apply( this, arguments ),
266 "slice", slice.call(arguments).join(",") );
269 map: function( callback ) {
270 return this.pushStack( jQuery.map(this, function( elem, i ) {
271 return callback.call( elem, i, elem );
276 return this.prevObject || jQuery(null);
279 // For internal use only.
280 // Behaves like an Array's method, not like a jQuery method.
286 // Give the init function the jQuery prototype for later instantiation
287 jQuery.fn.init.prototype = jQuery.fn;
289 jQuery.extend = jQuery.fn.extend = function() {
290 // copy reference to target object
291 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
293 // Handle a deep copy situation
294 if ( typeof target === "boolean" ) {
296 target = arguments[1] || {};
297 // skip the boolean and the target
301 // Handle case when target is a string or something (possible in deep copy)
302 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
306 // extend jQuery itself if only one argument is passed
307 if ( length === i ) {
312 for ( ; i < length; i++ ) {
313 // Only deal with non-null/undefined values
314 if ( (options = arguments[ i ]) != null ) {
315 // Extend the base object
316 for ( name in options ) {
317 src = target[ name ];
318 copy = options[ name ];
320 // Prevent never-ending loop
321 if ( target === copy ) {
325 // Recurse if we're merging object literal values or arrays
326 if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
327 var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
328 : jQuery.isArray(copy) ? [] : {};
330 // Never move original objects, clone them
331 target[ name ] = jQuery.extend( deep, clone, copy );
333 // Don't bring in undefined values
334 } else if ( copy !== undefined ) {
335 target[ name ] = copy;
341 // Return the modified object
346 noConflict: function( deep ) {
350 window.jQuery = _jQuery;
356 // Is the DOM ready to be used? Set to true once it occurs.
359 // Handle when the DOM is ready
361 // Make sure that the DOM is not already loaded
362 if ( !jQuery.isReady ) {
363 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
364 if ( !document.body ) {
365 return setTimeout( jQuery.ready, 13 );
368 // Remember that the DOM is ready
369 jQuery.isReady = true;
371 // If there are functions bound, to execute
373 // Execute all of them
375 while ( (fn = readyList[ i++ ]) ) {
376 fn.call( document, jQuery );
379 // Reset the list of functions
383 // Trigger any bound ready events
384 if ( jQuery.fn.triggerHandler ) {
385 jQuery( document ).triggerHandler( "ready" );
390 bindReady: function() {
397 // Catch cases where $(document).ready() is called after the
398 // browser event has already occurred.
399 if ( document.readyState === "complete" ) {
400 return jQuery.ready();
403 // Mozilla, Opera and webkit nightlies currently support this event
404 if ( document.addEventListener ) {
405 // Use the handy event callback
406 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
408 // A fallback to window.onload, that will always work
409 window.addEventListener( "load", jQuery.ready, false );
411 // If IE event model is used
412 } else if ( document.attachEvent ) {
413 // ensure firing before onload,
414 // maybe late but safe also for iframes
415 document.attachEvent("onreadystatechange", DOMContentLoaded);
417 // A fallback to window.onload, that will always work
418 window.attachEvent( "onload", jQuery.ready );
420 // If IE and not a frame
421 // continually check to see if the document is ready
422 var toplevel = false;
425 toplevel = window.frameElement == null;
428 if ( document.documentElement.doScroll && toplevel ) {
434 // See test/unit/core.js for details concerning isFunction.
435 // Since version 1.3, DOM methods and functions like alert
436 // aren't supported. They return false on IE (#2968).
437 isFunction: function( obj ) {
438 return toString.call(obj) === "[object Function]";
441 isArray: function( obj ) {
442 return toString.call(obj) === "[object Array]";
445 isPlainObject: function( obj ) {
446 // Must be an Object.
447 // Because of IE, we also have to check the presence of the constructor property.
448 // Make sure that DOM nodes and window objects don't pass through, as well
449 if ( !obj || toString.call(obj) !== "[object Object]" || obj.nodeType || obj.setInterval ) {
453 // Not own constructor property must be Object
454 if ( obj.constructor &&
455 !hasOwn.call(obj, "constructor") &&
456 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
460 // Own properties are enumerated firstly, so to speed up,
461 // if last one is own, then all properties are own.
464 for ( key in obj ) {}
466 return key === undefined || hasOwn.call( obj, key );
469 isEmptyObject: function( obj ) {
470 for ( var name in obj ) {
476 error: function( msg ) {
480 parseJSON: function( data ) {
481 if ( typeof data !== "string" || !data ) {
485 // Make sure leading/trailing whitespace is removed (IE can't handle it)
486 data = jQuery.trim( data );
488 // Make sure the incoming data is actual JSON
489 // Logic borrowed from http://json.org/json2.js
490 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
491 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
492 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
494 // Try to use the native JSON parser first
495 return window.JSON && window.JSON.parse ?
496 window.JSON.parse( data ) :
497 (new Function("return " + data))();
500 jQuery.error( "Invalid JSON: " + data );
506 // Evalulates a script in a global context
507 globalEval: function( data ) {
508 if ( data && rnotwhite.test(data) ) {
509 // Inspired by code by Andrea Giammarchi
510 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
511 var head = document.getElementsByTagName("head")[0] || document.documentElement,
512 script = document.createElement("script");
514 script.type = "text/javascript";
516 if ( jQuery.support.scriptEval ) {
517 script.appendChild( document.createTextNode( data ) );
522 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
523 // This arises when a base node is used (#2709).
524 head.insertBefore( script, head.firstChild );
525 head.removeChild( script );
529 nodeName: function( elem, name ) {
530 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
533 // args is for internal usage only
534 each: function( object, callback, args ) {
536 length = object.length,
537 isObj = length === undefined || jQuery.isFunction(object);
541 for ( name in object ) {
542 if ( callback.apply( object[ name ], args ) === false ) {
547 for ( ; i < length; ) {
548 if ( callback.apply( object[ i++ ], args ) === false ) {
554 // A special, fast, case for the most common use of each
557 for ( name in object ) {
558 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
563 for ( var value = object[0];
564 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
571 // Use native String.trim function wherever possible
574 return text == null ?
579 // Otherwise use our own trimming functionality
581 return text == null ?
583 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
586 // results is for internal usage only
587 makeArray: function( array, results ) {
588 var ret = results || [];
590 if ( array != null ) {
591 // The window, strings (and functions) also have 'length'
592 // The extra typeof function check is to prevent crashes
593 // in Safari 2 (See: #3039)
594 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
595 push.call( ret, array );
597 jQuery.merge( ret, array );
604 inArray: function( elem, array ) {
605 if ( array.indexOf ) {
606 return array.indexOf( elem );
609 for ( var i = 0, length = array.length; i < length; i++ ) {
610 if ( array[ i ] === elem ) {
618 merge: function( first, second ) {
619 var i = first.length, j = 0;
621 if ( typeof second.length === "number" ) {
622 for ( var l = second.length; j < l; j++ ) {
623 first[ i++ ] = second[ j ];
627 while ( second[j] !== undefined ) {
628 first[ i++ ] = second[ j++ ];
637 grep: function( elems, callback, inv ) {
638 var ret = [], retVal;
641 // Go through the array, only saving the items
642 // that pass the validator function
643 for ( var i = 0, length = elems.length; i < length; i++ ) {
644 retVal = !!callback( elems[ i ], i );
645 if ( inv !== retVal ) {
646 ret.push( elems[ i ] );
653 // arg is for internal usage only
654 map: function( elems, callback, arg ) {
657 // Go through the array, translating each of the items to their
658 // new value (or values).
659 for ( var i = 0, length = elems.length; i < length; i++ ) {
660 value = callback( elems[ i ], i, arg );
662 if ( value != null ) {
663 ret[ ret.length ] = value;
667 return ret.concat.apply( [], ret );
670 // A global GUID counter for objects
673 proxy: function( fn, proxy, thisObject ) {
674 if ( arguments.length === 2 ) {
675 if ( typeof proxy === "string" ) {
677 fn = thisObject[ proxy ];
680 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
686 if ( !proxy && fn ) {
688 return fn.apply( thisObject || this, arguments );
692 // Set the guid of unique handler to the same of original handler, so it can be removed
694 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
697 // So proxy can be declared as an argument
701 // Use of jQuery.browser is frowned upon.
702 // More details: http://docs.jquery.com/Utilities/jQuery.browser
703 uaMatch: function( ua ) {
704 ua = ua.toLowerCase();
706 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
707 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
708 /(msie) ([\w.]+)/.exec( ua ) ||
709 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
712 return { browser: match[1] || "", version: match[2] || "0" };
718 browserMatch = jQuery.uaMatch( userAgent );
719 if ( browserMatch.browser ) {
720 jQuery.browser[ browserMatch.browser ] = true;
721 jQuery.browser.version = browserMatch.version;
724 // Deprecated, use jQuery.browser.webkit instead
725 if ( jQuery.browser.webkit ) {
726 jQuery.browser.safari = true;
730 jQuery.inArray = function( elem, array ) {
731 return indexOf.call( array, elem );
735 // Verify that \s matches non-breaking spaces
736 // (IE fails on this test)
737 if ( !/\s/.test( "\xA0" ) ) {
738 trimLeft = /^[\s\xA0]+/;
739 trimRight = /[\s\xA0]+$/;
742 // All jQuery objects should point back to these
743 rootjQuery = jQuery(document);
745 // Cleanup functions for the document ready method
746 if ( document.addEventListener ) {
747 DOMContentLoaded = function() {
748 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
752 } else if ( document.attachEvent ) {
753 DOMContentLoaded = function() {
754 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
755 if ( document.readyState === "complete" ) {
756 document.detachEvent( "onreadystatechange", DOMContentLoaded );
762 // The DOM ready check for Internet Explorer
763 function doScrollCheck() {
764 if ( jQuery.isReady ) {
769 // If IE is used, use the trick by Diego Perini
770 // http://javascript.nwbox.com/IEContentLoaded/
771 document.documentElement.doScroll("left");
773 setTimeout( doScrollCheck, 1 );
777 // and execute any waiting functions
781 function evalScript( i, elem ) {
789 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
792 if ( elem.parentNode ) {
793 elem.parentNode.removeChild( elem );
797 // Mutifunctional method to get and set values to a collection
798 // The value/s can be optionally by executed if its a function
799 function access( elems, key, value, exec, fn, pass ) {
800 var length = elems.length;
802 // Setting many attributes
803 if ( typeof key === "object" ) {
804 for ( var k in key ) {
805 access( elems, k, key[k], exec, fn, value );
810 // Setting one attribute
811 if ( value !== undefined ) {
812 // Optionally, function values get executed if exec is true
813 exec = !pass && exec && jQuery.isFunction(value);
815 for ( var i = 0; i < length; i++ ) {
816 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
822 // Getting an attribute
823 return length ? fn( elems[0], key ) : undefined;
827 return (new Date()).getTime();