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.toLowerCase(),
38 // Has the ready events already been bound?
41 // The functions to execute on DOM ready
44 // The ready event handler
47 // Save a reference to some core methods
48 toString = Object.prototype.toString,
49 hasOwnProperty = Object.prototype.hasOwnProperty,
50 push = Array.prototype.push,
51 slice = Array.prototype.slice,
52 indexOf = Array.prototype.indexOf;
54 jQuery.fn = jQuery.prototype = {
55 init: function( selector, context ) {
56 var match, elem, ret, doc;
58 // Handle $(""), $(null), or $(undefined)
63 // Handle $(DOMElement)
64 if ( selector.nodeType ) {
65 this.context = this[0] = selector;
70 // Handle HTML strings
71 if ( typeof selector === "string" ) {
72 // Are we dealing with HTML string or an ID?
73 match = quickExpr.exec( selector );
75 // Verify a match, and that no context was specified for #id
76 if ( match && (match[1] || !context) ) {
78 // HANDLE: $(html) -> $(array)
80 doc = (context ? context.ownerDocument || context : document);
82 // If a single string is passed in and it's a single tag
83 // just do a createElement and skip the rest
84 ret = rsingleTag.exec( selector );
87 selector = [ doc.createElement( ret[1] ) ];
90 ret = buildFragment( [ match[1] ], [ doc ] );
91 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
96 elem = document.getElementById( match[2] );
99 // Handle the case where IE and Opera return items
100 // by name instead of ID
101 if ( elem.id !== match[2] ) {
102 return rootjQuery.find( selector );
105 // Otherwise, we inject the element directly into the jQuery object
110 this.context = document;
111 this.selector = selector;
116 } else if ( !context && /^\w+$/.test( selector ) ) {
117 this.selector = selector;
118 this.context = document;
119 selector = document.getElementsByTagName( selector );
121 // HANDLE: $(expr, $(...))
122 } else if ( !context || context.jquery ) {
123 return (context || rootjQuery).find( selector );
125 // HANDLE: $(expr, context)
126 // (which is just equivalent to: $(context).find(expr)
128 return jQuery( context ).find( selector );
131 // HANDLE: $(function)
132 // Shortcut for document ready
133 } else if ( jQuery.isFunction( selector ) ) {
134 return rootjQuery.ready( selector );
137 if (selector.selector !== undefined) {
138 this.selector = selector.selector;
139 this.context = selector.context;
142 return jQuery.isArray( selector ) ?
143 this.setArray( selector ) :
144 jQuery.makeArray( selector, this );
147 // Start with an empty selector
150 // The current version of jQuery being used
153 // The default length of a jQuery object is 0
156 // The number of elements contained in the matched element set
162 return slice.call( this, 0 );
165 // Get the Nth element in the matched element set OR
166 // Get the whole matched element set as a clean array
167 get: function( num ) {
170 // Return a 'clean' array
173 // Return just the object
174 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
177 // Take an array of elements and push it onto the stack
178 // (returning the new matched element set)
179 pushStack: function( elems, name, selector ) {
180 // Build a new jQuery matched element set
181 var ret = jQuery( elems || null );
183 // Add the old object onto the stack (as a reference)
184 ret.prevObject = this;
186 ret.context = this.context;
188 if ( name === "find" ) {
189 ret.selector = this.selector + (this.selector ? " " : "") + selector;
191 ret.selector = this.selector + "." + name + "(" + selector + ")";
194 // Return the newly-formed element set
198 // Force the current matched set of elements to become
199 // the specified array of elements (destroying the stack in the process)
200 // You should use pushStack() in order to do this, but maintain the stack
201 setArray: function( elems ) {
202 // Resetting the length to 0, then using the native Array push
203 // is a super-fast way to populate an object with array-like properties
205 push.apply( this, elems );
210 // Execute a callback for every element in the matched set.
211 // (You can seed the arguments with an array of args, but this is
212 // only used internally.)
213 each: function( callback, args ) {
214 return jQuery.each( this, callback, args );
217 ready: function( fn ) {
218 // Attach the listeners
221 // If the DOM is already ready
222 if ( jQuery.isReady ) {
223 // Execute the function immediately
224 fn.call( document, jQuery );
226 // Otherwise, remember the function for later
227 } else if ( readyList ) {
228 // Add the function to the wait list
229 readyList.push( fn );
238 this.slice( i, +i + 1 );
246 return this.eq( -1 );
250 return this.pushStack( slice.apply( this, arguments ),
251 "slice", slice.call(arguments).join(",") );
254 map: function( callback ) {
255 return this.pushStack( jQuery.map(this, function(elem, i){
256 return callback.call( elem, i, elem );
261 return this.prevObject || jQuery(null);
264 // For internal use only.
265 // Behaves like an Array's method, not like a jQuery method.
271 // Give the init function the jQuery prototype for later instantiation
272 jQuery.fn.init.prototype = jQuery.fn;
274 jQuery.extend = jQuery.fn.extend = function() {
275 // copy reference to target object
276 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
278 // Handle a deep copy situation
279 if ( typeof target === "boolean" ) {
281 target = arguments[1] || {};
282 // skip the boolean and the target
286 // Handle case when target is a string or something (possible in deep copy)
287 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
291 // extend jQuery itself if only one argument is passed
292 if ( length === i ) {
297 for ( ; i < length; i++ ) {
298 // Only deal with non-null/undefined values
299 if ( (options = arguments[ i ]) != null ) {
300 // Extend the base object
301 for ( name in options ) {
302 src = target[ name ];
303 copy = options[ name ];
305 // Prevent never-ending loop
306 if ( target === copy ) {
310 // Recurse if we're merging object literal values
311 if ( deep && copy && jQuery.isPlainObject(copy) ) {
312 // Don't extend not object literals
313 var clone = src && jQuery.isPlainObject(src) ? src : {};
315 // Never move original objects, clone them
316 target[ name ] = jQuery.extend( deep, clone, copy );
318 // Don't bring in undefined values
319 } else if ( copy !== undefined ) {
320 target[ name ] = copy;
326 // Return the modified object
331 noConflict: function( deep ) {
335 window.jQuery = _jQuery;
341 // Is the DOM ready to be used? Set to true once it occurs.
344 // Handle when the DOM is ready
346 // Make sure that the DOM is not already loaded
347 if ( !jQuery.isReady ) {
348 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
349 if ( !document.body ) {
350 return setTimeout( jQuery.ready, 13 );
353 // Remember that the DOM is ready
354 jQuery.isReady = true;
356 // If there are functions bound, to execute
358 // Execute all of them
360 while ( (fn = readyList[ i++ ]) ) {
361 fn.call( document, jQuery );
364 // Reset the list of functions
368 // Trigger any bound ready events
369 if ( jQuery.fn.triggerHandler ) {
370 jQuery( document ).triggerHandler( "ready" );
375 bindReady: function() {
376 if ( readyBound ) { return; }
379 // Catch cases where $(document).ready() is called after the
380 // browser event has already occurred.
381 if ( document.readyState === "complete" ) {
382 return jQuery.ready();
385 // Mozilla, Opera and webkit nightlies currently support this event
386 if ( document.addEventListener ) {
387 // Use the handy event callback
388 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
390 // A fallback to window.onload, that will always work
391 window.addEventListener( "load", jQuery.ready, false );
393 // If IE event model is used
394 } else if ( document.attachEvent ) {
395 // ensure firing before onload,
396 // maybe late but safe also for iframes
397 document.attachEvent("onreadystatechange", DOMContentLoaded);
399 // A fallback to window.onload, that will always work
400 window.attachEvent( "onload", jQuery.ready );
402 // If IE and not a frame
403 // continually check to see if the document is ready
404 var toplevel = false;
407 toplevel = window.frameElement == null;
410 if ( document.documentElement.doScroll && toplevel ) {
416 // See test/unit/core.js for details concerning isFunction.
417 // Since version 1.3, DOM methods and functions like alert
418 // aren't supported. They return false on IE (#2968).
419 isFunction: function( obj ) {
420 return toString.call(obj) === "[object Function]";
423 isArray: function( obj ) {
424 return toString.call(obj) === "[object Array]";
427 isPlainObject: function( obj ) {
428 if ( toString.call(obj) !== "[object Object]" || typeof obj.nodeType === "number" ) {
432 // not own constructor property must be Object
434 && !hasOwnProperty.call(obj, "constructor")
435 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
439 //own properties are iterated firstly,
440 //so to speed up, we can test last one if it is own or not
443 for ( key in obj ) {}
445 return key === undefined || hasOwnProperty.call( obj, key );
448 isEmptyObject: function( obj ) {
449 for ( var name in obj ) {
455 // Evalulates a script in a global context
456 globalEval: function( data ) {
457 if ( data && rnotwhite.test(data) ) {
458 // Inspired by code by Andrea Giammarchi
459 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
460 var head = document.getElementsByTagName("head")[0] || document.documentElement,
461 script = document.createElement("script");
463 script.type = "text/javascript";
465 if ( jQuery.support.scriptEval ) {
466 script.appendChild( document.createTextNode( data ) );
471 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
472 // This arises when a base node is used (#2709).
473 head.insertBefore( script, head.firstChild );
474 head.removeChild( script );
478 nodeName: function( elem, name ) {
479 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
482 // args is for internal usage only
483 each: function( object, callback, args ) {
485 length = object.length,
486 isObj = length === undefined || jQuery.isFunction(object);
490 for ( name in object ) {
491 if ( callback.apply( object[ name ], args ) === false ) {
496 for ( ; i < length; ) {
497 if ( callback.apply( object[ i++ ], args ) === false ) {
503 // A special, fast, case for the most common use of each
506 for ( name in object ) {
507 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
512 for ( var value = object[0];
513 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
520 trim: function( text ) {
521 return (text || "").replace( rtrim, "" );
524 // results is for internal usage only
525 makeArray: function( array, results ) {
526 var ret = results || [];
528 if ( array != null ) {
529 // The window, strings (and functions) also have 'length'
530 // The extra typeof function check is to prevent crashes
531 // in Safari 2 (See: #3039)
532 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
533 push.call( ret, array );
535 jQuery.merge( ret, array );
542 inArray: function( elem, array ) {
543 if ( array.indexOf ) {
544 return array.indexOf( elem );
547 for ( var i = 0, length = array.length; i < length; i++ ) {
548 if ( array[ i ] === elem ) {
556 merge: function( first, second ) {
557 var i = first.length, j = 0;
559 if ( typeof second.length === "number" ) {
560 for ( var l = second.length; j < l; j++ ) {
561 first[ i++ ] = second[ j ];
564 while ( second[j] !== undefined ) {
565 first[ i++ ] = second[ j++ ];
574 grep: function( elems, callback, inv ) {
577 // Go through the array, only saving the items
578 // that pass the validator function
579 for ( var i = 0, length = elems.length; i < length; i++ ) {
580 if ( !inv !== !callback( elems[ i ], i ) ) {
581 ret.push( elems[ i ] );
588 // arg is for internal usage only
589 map: function( elems, callback, arg ) {
592 // Go through the array, translating each of the items to their
593 // new value (or values).
594 for ( var i = 0, length = elems.length; i < length; i++ ) {
595 value = callback( elems[ i ], i, arg );
597 if ( value != null ) {
598 ret[ ret.length ] = value;
602 return ret.concat.apply( [], ret );
605 // Use of jQuery.browser is frowned upon.
606 // More details: http://docs.jquery.com/Utilities/jQuery.browser
608 version: (/.*?(?:firefox|safari|opera|msie)[\/ ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
609 safari: /safari/.test( userAgent ),
610 opera: /opera/.test( userAgent ),
611 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
612 firefox: /firefox/.test( userAgent )
617 jQuery.browser.mozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
620 jQuery.inArray = function( elem, array ) {
621 return indexOf.call( array, elem );
625 // All jQuery objects should point back to these
626 rootjQuery = jQuery(document);
628 // Cleanup functions for the document ready method
629 if ( document.addEventListener ) {
630 DOMContentLoaded = function() {
631 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
635 } else if ( document.attachEvent ) {
636 DOMContentLoaded = function() {
637 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
638 if ( document.readyState === "complete" ) {
639 document.detachEvent( "onreadystatechange", DOMContentLoaded );
645 // The DOM ready check for Internet Explorer
646 function doScrollCheck() {
647 if ( jQuery.isReady ) {
652 // If IE is used, use the trick by Diego Perini
653 // http://javascript.nwbox.com/IEContentLoaded/
654 document.documentElement.doScroll("left");
656 setTimeout( doScrollCheck, 1 );
660 // and execute any waiting functions
665 jQuery.inArray = function( elem, array ) {
666 return indexOf.call( array, elem );
670 function evalScript( i, elem ) {
678 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
681 if ( elem.parentNode ) {
682 elem.parentNode.removeChild( elem );
686 // Mutifunctional method to get and set values to a collection
687 // The value/s can be optionally by executed if its a function
688 function access( elems, key, value, exec, fn ) {
689 var length = elems.length;
691 // Setting many attributes
692 if ( typeof key === "object" ) {
693 for ( var k in key ) {
694 access( elems, k, key[k], exec, fn );
699 // Setting one attribute
700 if ( value !== undefined ) {
701 // Optionally, function values get executed if exec is true
702 exec = exec && jQuery.isFunction(value);
704 for ( var i = 0; i < length; i++ ) {
705 fn( elems[i], key, exec ? value.call( elems[i], i ) : value );
711 // Getting an attribute
712 return length ? fn( elems[0], key ) : null;
716 return (new Date).getTime();