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, parent;
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 parent = ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment;
94 while ( parent.firstChild ) {
95 selector.push( parent.removeChild( parent.firstChild ) );
101 elem = document.getElementById( match[2] );
104 // Handle the case where IE and Opera return items
105 // by name instead of ID
106 if ( elem.id !== match[2] ) {
107 return rootjQuery.find( selector );
110 // Otherwise, we inject the element directly into the jQuery object
115 this.context = document;
116 this.selector = selector;
121 } else if ( !context && /^\w+$/.test( selector ) ) {
122 this.selector = selector;
123 this.context = document;
124 selector = document.getElementsByTagName( selector );
126 // HANDLE: $(expr, $(...))
127 } else if ( !context || context.jquery ) {
128 return (context || rootjQuery).find( selector );
130 // HANDLE: $(expr, context)
131 // (which is just equivalent to: $(context).find(expr)
133 return jQuery( context ).find( selector );
136 // HANDLE: $(function)
137 // Shortcut for document ready
138 } else if ( jQuery.isFunction( selector ) ) {
139 return rootjQuery.ready( selector );
142 if (selector.selector !== undefined) {
143 this.selector = selector.selector;
144 this.context = selector.context;
147 return jQuery.isArray( selector ) ?
148 this.setArray( selector ) :
149 jQuery.makeArray( selector, this );
152 // Start with an empty selector
155 // The current version of jQuery being used
158 // The default length of a jQuery object is 0
161 // The number of elements contained in the matched element set
167 return slice.call( this, 0 );
170 // Get the Nth element in the matched element set OR
171 // Get the whole matched element set as a clean array
172 get: function( num ) {
175 // Return a 'clean' array
178 // Return just the object
179 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
182 // Take an array of elements and push it onto the stack
183 // (returning the new matched element set)
184 pushStack: function( elems, name, selector ) {
185 // Build a new jQuery matched element set
186 var ret = jQuery( elems || null );
188 // Add the old object onto the stack (as a reference)
189 ret.prevObject = this;
191 ret.context = this.context;
193 if ( name === "find" ) {
194 ret.selector = this.selector + (this.selector ? " " : "") + selector;
196 ret.selector = this.selector + "." + name + "(" + selector + ")";
199 // Return the newly-formed element set
203 // Force the current matched set of elements to become
204 // the specified array of elements (destroying the stack in the process)
205 // You should use pushStack() in order to do this, but maintain the stack
206 setArray: function( elems ) {
207 // Resetting the length to 0, then using the native Array push
208 // is a super-fast way to populate an object with array-like properties
210 push.apply( this, elems );
215 // Execute a callback for every element in the matched set.
216 // (You can seed the arguments with an array of args, but this is
217 // only used internally.)
218 each: function( callback, args ) {
219 return jQuery.each( this, callback, args );
222 ready: function( fn ) {
223 // Attach the listeners
226 // If the DOM is already ready
227 if ( jQuery.isReady ) {
228 // Execute the function immediately
229 fn.call( document, jQuery );
231 // Otherwise, remember the function for later
232 } else if ( readyList ) {
233 // Add the function to the wait list
234 readyList.push( fn );
243 this.slice( i, +i + 1 );
251 return this.eq( -1 );
255 return this.pushStack( slice.apply( this, arguments ),
256 "slice", slice.call(arguments).join(",") );
259 map: function( callback ) {
260 return this.pushStack( jQuery.map(this, function(elem, i){
261 return callback.call( elem, i, elem );
266 return this.prevObject || jQuery(null);
269 // For internal use only.
270 // Behaves like an Array's method, not like a jQuery method.
276 // Give the init function the jQuery prototype for later instantiation
277 jQuery.fn.init.prototype = jQuery.fn;
279 jQuery.extend = jQuery.fn.extend = function() {
280 // copy reference to target object
281 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
283 // Handle a deep copy situation
284 if ( typeof target === "boolean" ) {
286 target = arguments[1] || {};
287 // skip the boolean and the target
291 // Handle case when target is a string or something (possible in deep copy)
292 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
296 // extend jQuery itself if only one argument is passed
297 if ( length === i ) {
302 for ( ; i < length; i++ ) {
303 // Only deal with non-null/undefined values
304 if ( (options = arguments[ i ]) != null ) {
305 // Extend the base object
306 for ( name in options ) {
307 src = target[ name ];
308 copy = options[ name ];
310 // Prevent never-ending loop
311 if ( target === copy ) {
315 // Recurse if we're merging object literal values
316 if ( deep && copy && jQuery.isPlainObject(copy) ) {
317 // Don't extend not object literals
318 var clone = src && jQuery.isPlainObject(src) ? src : {};
320 // Never move original objects, clone them
321 target[ name ] = jQuery.extend( deep, clone, copy );
323 // Don't bring in undefined values
324 } else if ( copy !== undefined ) {
325 target[ name ] = copy;
331 // Return the modified object
336 noConflict: function( deep ) {
340 window.jQuery = _jQuery;
346 // Is the DOM ready to be used? Set to true once it occurs.
349 // Handle when the DOM is ready
351 // Make sure that the DOM is not already loaded
352 if ( !jQuery.isReady ) {
353 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
354 if ( !document.body ) {
355 return setTimeout( jQuery.ready, 13 );
358 // Remember that the DOM is ready
359 jQuery.isReady = true;
361 // If there are functions bound, to execute
363 // Execute all of them
365 while ( (fn = readyList[ i++ ]) ) {
366 fn.call( document, jQuery );
369 // Reset the list of functions
373 // Trigger any bound ready events
374 if ( jQuery.fn.triggerHandler ) {
375 jQuery( document ).triggerHandler( "ready" );
380 bindReady: function() {
381 if ( readyBound ) { return; }
384 // Catch cases where $(document).ready() is called after the
385 // browser event has already occurred.
386 if ( document.readyState === "complete" ) {
387 return jQuery.ready();
390 // Mozilla, Opera and webkit nightlies currently support this event
391 if ( document.addEventListener ) {
392 // Use the handy event callback
393 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
395 // A fallback to window.onload, that will always work
396 window.addEventListener( "load", jQuery.ready, false );
398 // If IE event model is used
399 } else if ( document.attachEvent ) {
400 // ensure firing before onload,
401 // maybe late but safe also for iframes
402 document.attachEvent("onreadystatechange", DOMContentLoaded);
404 // A fallback to window.onload, that will always work
405 window.attachEvent( "onload", jQuery.ready );
407 // If IE and not a frame
408 // continually check to see if the document is ready
409 var toplevel = false;
412 toplevel = window.frameElement == null;
415 if ( document.documentElement.doScroll && toplevel ) {
421 // See test/unit/core.js for details concerning isFunction.
422 // Since version 1.3, DOM methods and functions like alert
423 // aren't supported. They return false on IE (#2968).
424 isFunction: function( obj ) {
425 return toString.call(obj) === "[object Function]";
428 isArray: function( obj ) {
429 return toString.call(obj) === "[object Array]";
432 isPlainObject: function( obj ) {
433 if ( toString.call(obj) !== "[object Object]" || typeof obj.nodeType === "number" ) {
437 // not own constructor property must be Object
439 && !hasOwnProperty.call(obj, "constructor")
440 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
444 //own properties are iterated firstly,
445 //so to speed up, we can test last one if it is own or not
448 for ( key in obj ) {}
450 return key === undefined || hasOwnProperty.call( obj, key );
453 isEmptyObject: function( obj ) {
454 for ( var name in obj ) {
460 // Evalulates a script in a global context
461 globalEval: function( data ) {
462 if ( data && rnotwhite.test(data) ) {
463 // Inspired by code by Andrea Giammarchi
464 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
465 var head = document.getElementsByTagName("head")[0] || document.documentElement,
466 script = document.createElement("script");
468 script.type = "text/javascript";
470 if ( jQuery.support.scriptEval ) {
471 script.appendChild( document.createTextNode( data ) );
476 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
477 // This arises when a base node is used (#2709).
478 head.insertBefore( script, head.firstChild );
479 head.removeChild( script );
483 nodeName: function( elem, name ) {
484 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
487 // args is for internal usage only
488 each: function( object, callback, args ) {
490 length = object.length,
491 isObj = length === undefined || jQuery.isFunction(object);
495 for ( name in object ) {
496 if ( callback.apply( object[ name ], args ) === false ) {
501 for ( ; i < length; ) {
502 if ( callback.apply( object[ i++ ], args ) === false ) {
508 // A special, fast, case for the most common use of each
511 for ( name in object ) {
512 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
517 for ( var value = object[0];
518 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
525 trim: function( text ) {
526 return (text || "").replace( rtrim, "" );
529 // results is for internal usage only
530 makeArray: function( array, results ) {
531 var ret = results || [];
533 if ( array != null ) {
534 // The window, strings (and functions) also have 'length'
535 // The extra typeof function check is to prevent crashes
536 // in Safari 2 (See: #3039)
537 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
538 push.call( ret, array );
540 jQuery.merge( ret, array );
547 inArray: function( elem, array ) {
548 if ( array.indexOf ) {
549 return array.indexOf( elem );
552 for ( var i = 0, length = array.length; i < length; i++ ) {
553 if ( array[ i ] === elem ) {
561 merge: function( first, second ) {
562 var i = first.length, j = 0;
564 if ( typeof second.length === "number" ) {
565 for ( var l = second.length; j < l; j++ ) {
566 first[ i++ ] = second[ j ];
569 while ( second[j] !== undefined ) {
570 first[ i++ ] = second[ j++ ];
579 grep: function( elems, callback, inv ) {
582 // Go through the array, only saving the items
583 // that pass the validator function
584 for ( var i = 0, length = elems.length; i < length; i++ ) {
585 if ( !inv !== !callback( elems[ i ], i ) ) {
586 ret.push( elems[ i ] );
593 // arg is for internal usage only
594 map: function( elems, callback, arg ) {
597 // Go through the array, translating each of the items to their
598 // new value (or values).
599 for ( var i = 0, length = elems.length; i < length; i++ ) {
600 value = callback( elems[ i ], i, arg );
602 if ( value != null ) {
603 ret[ ret.length ] = value;
607 return ret.concat.apply( [], ret );
610 // Use of jQuery.browser is frowned upon.
611 // More details: http://docs.jquery.com/Utilities/jQuery.browser
613 version: (/.*?(?:firefox|safari|opera|msie)[\/ ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
614 safari: /safari/.test( userAgent ),
615 opera: /opera/.test( userAgent ),
616 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
617 firefox: /firefox/.test( userAgent )
622 jQuery.browser.mozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
625 jQuery.inArray = function( elem, array ) {
626 return indexOf.call( array, elem );
630 // All jQuery objects should point back to these
631 rootjQuery = jQuery(document);
633 // Cleanup functions for the document ready method
634 if ( document.addEventListener ) {
635 DOMContentLoaded = function() {
636 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
640 } else if ( document.attachEvent ) {
641 DOMContentLoaded = function() {
642 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
643 if ( document.readyState === "complete" ) {
644 document.detachEvent( "onreadystatechange", DOMContentLoaded );
650 // The DOM ready check for Internet Explorer
651 function doScrollCheck() {
652 if ( jQuery.isReady ) {
657 // If IE is used, use the trick by Diego Perini
658 // http://javascript.nwbox.com/IEContentLoaded/
659 document.documentElement.doScroll("left");
661 setTimeout( doScrollCheck, 1 );
665 // and execute any waiting functions
670 jQuery.inArray = function( elem, array ) {
671 return indexOf.call( array, elem );
675 function evalScript( i, elem ) {
683 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
686 if ( elem.parentNode ) {
687 elem.parentNode.removeChild( elem );
691 // Mutifunctional method to get and set values to a collection
692 // The value/s can be optionally by executed if its a function
693 function access( elems, key, value, exec, fn ) {
694 var length = elems.length;
696 // Setting many attributes
697 if ( typeof key === "object" ) {
698 for ( var k in key ) {
699 access( elems, k, key[k], exec, fn );
704 // Setting one attribute
705 if ( value !== undefined ) {
706 // Optionally, function values get executed if exec is true
707 exec = exec && jQuery.isFunction(value);
709 for ( var i = 0; i < length; i++ ) {
710 fn( elems[i], key, exec ? value.call( elems[i], i ) : value );
716 // Getting an attribute
717 return length ? fn( elems[0], key ) : null;
721 return (new Date).getTime();