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: function( obj ) {
445 return jQuery.type(obj) === "array";
448 type: function( obj ) {
449 return toString.call(obj).slice(8, -1).toLowerCase();
452 isPlainObject: function( obj ) {
453 // Must be an Object.
454 // Because of IE, we also have to check the presence of the constructor property.
455 // Make sure that DOM nodes and window objects don't pass through, as well
456 if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || obj.setInterval ) {
460 // Not own constructor property must be Object
461 if ( obj.constructor &&
462 !hasOwn.call(obj, "constructor") &&
463 !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
467 // Own properties are enumerated firstly, so to speed up,
468 // if last one is own, then all properties are own.
471 for ( key in obj ) {}
473 return key === undefined || hasOwn.call( obj, key );
476 isEmptyObject: function( obj ) {
477 for ( var name in obj ) {
483 error: function( msg ) {
487 parseJSON: function( data ) {
488 if ( typeof data !== "string" || !data ) {
492 // Make sure leading/trailing whitespace is removed (IE can't handle it)
493 data = jQuery.trim( data );
495 // Make sure the incoming data is actual JSON
496 // Logic borrowed from http://json.org/json2.js
497 if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
498 .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
499 .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
501 // Try to use the native JSON parser first
502 return window.JSON && window.JSON.parse ?
503 window.JSON.parse( data ) :
504 (new Function("return " + data))();
507 jQuery.error( "Invalid JSON: " + data );
513 // Evalulates a script in a global context
514 globalEval: function( data ) {
515 if ( data && rnotwhite.test(data) ) {
516 // Inspired by code by Andrea Giammarchi
517 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
518 var head = document.getElementsByTagName("head")[0] || document.documentElement,
519 script = document.createElement("script");
521 script.type = "text/javascript";
523 if ( jQuery.support.scriptEval ) {
524 script.appendChild( document.createTextNode( data ) );
529 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
530 // This arises when a base node is used (#2709).
531 head.insertBefore( script, head.firstChild );
532 head.removeChild( script );
536 nodeName: function( elem, name ) {
537 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
540 // args is for internal usage only
541 each: function( object, callback, args ) {
543 length = object.length,
544 isObj = length === undefined || jQuery.isFunction(object);
548 for ( name in object ) {
549 if ( callback.apply( object[ name ], args ) === false ) {
554 for ( ; i < length; ) {
555 if ( callback.apply( object[ i++ ], args ) === false ) {
561 // A special, fast, case for the most common use of each
564 for ( name in object ) {
565 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
570 for ( var value = object[0];
571 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
578 // Use native String.trim function wherever possible
581 return text == null ?
586 // Otherwise use our own trimming functionality
588 return text == null ?
590 text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
593 // results is for internal usage only
594 makeArray: function( array, results ) {
595 var ret = results || [];
597 if ( array != null ) {
598 // The window, strings (and functions) also have 'length'
599 // The extra typeof function check is to prevent crashes
600 // in Safari 2 (See: #3039)
601 // Tweaked logic slightly to handle Blackberry 4.7 RegExp issues #6930
602 var type = jQuery.type(array);
604 if ( array.length == null || type === "string" || type === "function" || type === "regexp" || (typeof type !== "function" && array.setInterval) ) {
605 push.call( ret, array );
607 jQuery.merge( ret, array );
614 inArray: function( elem, array ) {
615 if ( array.indexOf ) {
616 return array.indexOf( elem );
619 for ( var i = 0, length = array.length; i < length; i++ ) {
620 if ( array[ i ] === elem ) {
628 merge: function( first, second ) {
629 var i = first.length, j = 0;
631 if ( typeof second.length === "number" ) {
632 for ( var l = second.length; j < l; j++ ) {
633 first[ i++ ] = second[ j ];
637 while ( second[j] !== undefined ) {
638 first[ i++ ] = second[ j++ ];
647 grep: function( elems, callback, inv ) {
648 var ret = [], retVal;
651 // Go through the array, only saving the items
652 // that pass the validator function
653 for ( var i = 0, length = elems.length; i < length; i++ ) {
654 retVal = !!callback( elems[ i ], i );
655 if ( inv !== retVal ) {
656 ret.push( elems[ i ] );
663 // arg is for internal usage only
664 map: function( elems, callback, arg ) {
667 // Go through the array, translating each of the items to their
668 // new value (or values).
669 for ( var i = 0, length = elems.length; i < length; i++ ) {
670 value = callback( elems[ i ], i, arg );
672 if ( value != null ) {
673 ret[ ret.length ] = value;
677 return ret.concat.apply( [], ret );
680 // A global GUID counter for objects
683 proxy: function( fn, proxy, thisObject ) {
684 if ( arguments.length === 2 ) {
685 if ( typeof proxy === "string" ) {
687 fn = thisObject[ proxy ];
690 } else if ( proxy && !jQuery.isFunction( proxy ) ) {
696 if ( !proxy && fn ) {
698 return fn.apply( thisObject || this, arguments );
702 // Set the guid of unique handler to the same of original handler, so it can be removed
704 proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
707 // So proxy can be declared as an argument
711 // Mutifunctional method to get and set values to a collection
712 // The value/s can be optionally by executed if its a function
713 access: function( elems, key, value, exec, fn, pass ) {
714 var length = elems.length;
716 // Setting many attributes
717 if ( typeof key === "object" ) {
718 for ( var k in key ) {
719 jQuery.access( elems, k, key[k], exec, fn, value );
724 // Setting one attribute
725 if ( value !== undefined ) {
726 // Optionally, function values get executed if exec is true
727 exec = !pass && exec && jQuery.isFunction(value);
729 for ( var i = 0; i < length; i++ ) {
730 fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
736 // Getting an attribute
737 return length ? fn( elems[0], key ) : undefined;
741 return (new Date()).getTime();
744 // Use of jQuery.browser is frowned upon.
745 // More details: http://docs.jquery.com/Utilities/jQuery.browser
746 uaMatch: function( ua ) {
747 ua = ua.toLowerCase();
749 var match = /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
750 /(opera)(?:.*version)?[ \/]([\w.]+)/.exec( ua ) ||
751 /(msie) ([\w.]+)/.exec( ua ) ||
752 !/compatible/.test( ua ) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec( ua ) ||
755 return { browser: match[1] || "", version: match[2] || "0" };
761 browserMatch = jQuery.uaMatch( userAgent );
762 if ( browserMatch.browser ) {
763 jQuery.browser[ browserMatch.browser ] = true;
764 jQuery.browser.version = browserMatch.version;
767 // Deprecated, use jQuery.browser.webkit instead
768 if ( jQuery.browser.webkit ) {
769 jQuery.browser.safari = true;
773 jQuery.inArray = function( elem, array ) {
774 return indexOf.call( array, elem );
778 // Verify that \s matches non-breaking spaces
779 // (IE fails on this test)
780 if ( !/\s/.test( "\xA0" ) ) {
781 trimLeft = /^[\s\xA0]+/;
782 trimRight = /[\s\xA0]+$/;
785 // All jQuery objects should point back to these
786 rootjQuery = jQuery(document);
788 // Cleanup functions for the document ready method
789 if ( document.addEventListener ) {
790 DOMContentLoaded = function() {
791 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
795 } else if ( document.attachEvent ) {
796 DOMContentLoaded = function() {
797 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
798 if ( document.readyState === "complete" ) {
799 document.detachEvent( "onreadystatechange", DOMContentLoaded );
805 // The DOM ready check for Internet Explorer
806 function doScrollCheck() {
807 if ( jQuery.isReady ) {
812 // If IE is used, use the trick by Diego Perini
813 // http://javascript.nwbox.com/IEContentLoaded/
814 document.documentElement.doScroll("left");
816 setTimeout( doScrollCheck, 1 );
820 // and execute any waiting functions
824 // Expose jQuery to the global object
825 window.jQuery = window.$ = jQuery;