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 if ( jQuery.isPlainObject( context ) ) {
88 selector = [ document.createElement( ret[1] ) ];
89 jQuery.fn.attr.call( selector, context, true );
92 selector = [ doc.createElement( ret[1] ) ];
96 ret = buildFragment( [ match[1] ], [ doc ] );
97 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
102 elem = document.getElementById( match[2] );
105 // Handle the case where IE and Opera return items
106 // by name instead of ID
107 if ( elem.id !== match[2] ) {
108 return rootjQuery.find( selector );
111 // Otherwise, we inject the element directly into the jQuery object
116 this.context = document;
117 this.selector = selector;
122 } else if ( !context && /^\w+$/.test( selector ) ) {
123 this.selector = selector;
124 this.context = document;
125 selector = document.getElementsByTagName( selector );
127 // HANDLE: $(expr, $(...))
128 } else if ( !context || context.jquery ) {
129 return (context || rootjQuery).find( selector );
131 // HANDLE: $(expr, context)
132 // (which is just equivalent to: $(context).find(expr)
134 return jQuery( context ).find( selector );
137 // HANDLE: $(function)
138 // Shortcut for document ready
139 } else if ( jQuery.isFunction( selector ) ) {
140 return rootjQuery.ready( selector );
143 if (selector.selector !== undefined) {
144 this.selector = selector.selector;
145 this.context = selector.context;
148 return jQuery.isArray( selector ) ?
149 this.setArray( selector ) :
150 jQuery.makeArray( selector, this );
153 // Start with an empty selector
156 // The current version of jQuery being used
159 // The default length of a jQuery object is 0
162 // The number of elements contained in the matched element set
168 return slice.call( this, 0 );
171 // Get the Nth element in the matched element set OR
172 // Get the whole matched element set as a clean array
173 get: function( num ) {
176 // Return a 'clean' array
179 // Return just the object
180 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
183 // Take an array of elements and push it onto the stack
184 // (returning the new matched element set)
185 pushStack: function( elems, name, selector ) {
186 // Build a new jQuery matched element set
187 var ret = jQuery( elems || null );
189 // Add the old object onto the stack (as a reference)
190 ret.prevObject = this;
192 ret.context = this.context;
194 if ( name === "find" ) {
195 ret.selector = this.selector + (this.selector ? " " : "") + selector;
197 ret.selector = this.selector + "." + name + "(" + selector + ")";
200 // Return the newly-formed element set
204 // Force the current matched set of elements to become
205 // the specified array of elements (destroying the stack in the process)
206 // You should use pushStack() in order to do this, but maintain the stack
207 setArray: function( elems ) {
208 // Resetting the length to 0, then using the native Array push
209 // is a super-fast way to populate an object with array-like properties
211 push.apply( this, elems );
216 // Execute a callback for every element in the matched set.
217 // (You can seed the arguments with an array of args, but this is
218 // only used internally.)
219 each: function( callback, args ) {
220 return jQuery.each( this, callback, args );
223 ready: function( fn ) {
224 // Attach the listeners
227 // If the DOM is already ready
228 if ( jQuery.isReady ) {
229 // Execute the function immediately
230 fn.call( document, jQuery );
232 // Otherwise, remember the function for later
233 } else if ( readyList ) {
234 // Add the function to the wait list
235 readyList.push( fn );
244 this.slice( i, +i + 1 );
252 return this.eq( -1 );
256 return this.pushStack( slice.apply( this, arguments ),
257 "slice", slice.call(arguments).join(",") );
260 map: function( callback ) {
261 return this.pushStack( jQuery.map(this, function(elem, i){
262 return callback.call( elem, i, elem );
267 return this.prevObject || jQuery(null);
270 // For internal use only.
271 // Behaves like an Array's method, not like a jQuery method.
277 // Give the init function the jQuery prototype for later instantiation
278 jQuery.fn.init.prototype = jQuery.fn;
280 jQuery.extend = jQuery.fn.extend = function() {
281 // copy reference to target object
282 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
284 // Handle a deep copy situation
285 if ( typeof target === "boolean" ) {
287 target = arguments[1] || {};
288 // skip the boolean and the target
292 // Handle case when target is a string or something (possible in deep copy)
293 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
297 // extend jQuery itself if only one argument is passed
298 if ( length === i ) {
303 for ( ; i < length; i++ ) {
304 // Only deal with non-null/undefined values
305 if ( (options = arguments[ i ]) != null ) {
306 // Extend the base object
307 for ( name in options ) {
308 src = target[ name ];
309 copy = options[ name ];
311 // Prevent never-ending loop
312 if ( target === copy ) {
316 // Recurse if we're merging object literal values
317 if ( deep && copy && jQuery.isPlainObject(copy) ) {
318 // Don't extend not object literals
319 var clone = src && jQuery.isPlainObject(src) ? src : {};
321 // Never move original objects, clone them
322 target[ name ] = jQuery.extend( deep, clone, copy );
324 // Don't bring in undefined values
325 } else if ( copy !== undefined ) {
326 target[ name ] = copy;
332 // Return the modified object
337 noConflict: function( deep ) {
341 window.jQuery = _jQuery;
347 // Is the DOM ready to be used? Set to true once it occurs.
350 // Handle when the DOM is ready
352 // Make sure that the DOM is not already loaded
353 if ( !jQuery.isReady ) {
354 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
355 if ( !document.body ) {
356 return setTimeout( jQuery.ready, 13 );
359 // Remember that the DOM is ready
360 jQuery.isReady = true;
362 // If there are functions bound, to execute
364 // Execute all of them
366 while ( (fn = readyList[ i++ ]) ) {
367 fn.call( document, jQuery );
370 // Reset the list of functions
374 // Trigger any bound ready events
375 if ( jQuery.fn.triggerHandler ) {
376 jQuery( document ).triggerHandler( "ready" );
381 bindReady: function() {
382 if ( readyBound ) { return; }
385 // Catch cases where $(document).ready() is called after the
386 // browser event has already occurred.
387 if ( document.readyState === "complete" ) {
388 return jQuery.ready();
391 // Mozilla, Opera and webkit nightlies currently support this event
392 if ( document.addEventListener ) {
393 // Use the handy event callback
394 document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
396 // A fallback to window.onload, that will always work
397 window.addEventListener( "load", jQuery.ready, false );
399 // If IE event model is used
400 } else if ( document.attachEvent ) {
401 // ensure firing before onload,
402 // maybe late but safe also for iframes
403 document.attachEvent("onreadystatechange", DOMContentLoaded);
405 // A fallback to window.onload, that will always work
406 window.attachEvent( "onload", jQuery.ready );
408 // If IE and not a frame
409 // continually check to see if the document is ready
410 var toplevel = false;
413 toplevel = window.frameElement == null;
416 if ( document.documentElement.doScroll && toplevel ) {
422 // See test/unit/core.js for details concerning isFunction.
423 // Since version 1.3, DOM methods and functions like alert
424 // aren't supported. They return false on IE (#2968).
425 isFunction: function( obj ) {
426 return toString.call(obj) === "[object Function]";
429 isArray: function( obj ) {
430 return toString.call(obj) === "[object Array]";
433 isPlainObject: function( obj ) {
434 // Must be an Object.
435 // Because of IE, we also have to check the presence of the constructor property.
436 // Make sure that DOM nodes and window objects don't pass through, as well
437 if ( !obj || toString.call(obj) !== "[object Object]" || !("constructor" in obj) || obj.nodeType || obj.setInterval ) {
441 // Not own constructor property must be Object
443 && !hasOwnProperty.call(obj, "constructor")
444 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
448 // Own properties are enumerated firstly, so to speed up,
449 // if last one is own, then all properties are own.
452 for ( key in obj ) {}
454 return key === undefined || hasOwnProperty.call( obj, key );
457 isEmptyObject: function( obj ) {
458 for ( var name in obj ) {
464 // Evalulates a script in a global context
465 globalEval: function( data ) {
466 if ( data && rnotwhite.test(data) ) {
467 // Inspired by code by Andrea Giammarchi
468 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
469 var head = document.getElementsByTagName("head")[0] || document.documentElement,
470 script = document.createElement("script");
472 script.type = "text/javascript";
474 if ( jQuery.support.scriptEval ) {
475 script.appendChild( document.createTextNode( data ) );
480 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
481 // This arises when a base node is used (#2709).
482 head.insertBefore( script, head.firstChild );
483 head.removeChild( script );
487 nodeName: function( elem, name ) {
488 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
491 // args is for internal usage only
492 each: function( object, callback, args ) {
494 length = object.length,
495 isObj = length === undefined || jQuery.isFunction(object);
499 for ( name in object ) {
500 if ( callback.apply( object[ name ], args ) === false ) {
505 for ( ; i < length; ) {
506 if ( callback.apply( object[ i++ ], args ) === false ) {
512 // A special, fast, case for the most common use of each
515 for ( name in object ) {
516 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
521 for ( var value = object[0];
522 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
529 trim: function( text ) {
530 return (text || "").replace( rtrim, "" );
533 // results is for internal usage only
534 makeArray: function( array, results ) {
535 var ret = results || [];
537 if ( array != null ) {
538 // The window, strings (and functions) also have 'length'
539 // The extra typeof function check is to prevent crashes
540 // in Safari 2 (See: #3039)
541 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
542 push.call( ret, array );
544 jQuery.merge( ret, array );
551 inArray: function( elem, array ) {
552 if ( array.indexOf ) {
553 return array.indexOf( elem );
556 for ( var i = 0, length = array.length; i < length; i++ ) {
557 if ( array[ i ] === elem ) {
565 merge: function( first, second ) {
566 var i = first.length, j = 0;
568 if ( typeof second.length === "number" ) {
569 for ( var l = second.length; j < l; j++ ) {
570 first[ i++ ] = second[ j ];
573 while ( second[j] !== undefined ) {
574 first[ i++ ] = second[ j++ ];
583 grep: function( elems, callback, inv ) {
586 // Go through the array, only saving the items
587 // that pass the validator function
588 for ( var i = 0, length = elems.length; i < length; i++ ) {
589 if ( !inv !== !callback( elems[ i ], i ) ) {
590 ret.push( elems[ i ] );
597 // arg is for internal usage only
598 map: function( elems, callback, arg ) {
601 // Go through the array, translating each of the items to their
602 // new value (or values).
603 for ( var i = 0, length = elems.length; i < length; i++ ) {
604 value = callback( elems[ i ], i, arg );
606 if ( value != null ) {
607 ret[ ret.length ] = value;
611 return ret.concat.apply( [], ret );
614 // Use of jQuery.browser is frowned upon.
615 // More details: http://docs.jquery.com/Utilities/jQuery.browser
617 version: (/.*?(?:firefox|safari|opera|msie)[\/ ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
618 safari: /safari/.test( userAgent ),
619 opera: /opera/.test( userAgent ),
620 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
621 firefox: /firefox/.test( userAgent )
626 jQuery.browser.mozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
629 jQuery.inArray = function( elem, array ) {
630 return indexOf.call( array, elem );
634 // All jQuery objects should point back to these
635 rootjQuery = jQuery(document);
637 // Cleanup functions for the document ready method
638 if ( document.addEventListener ) {
639 DOMContentLoaded = function() {
640 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
644 } else if ( document.attachEvent ) {
645 DOMContentLoaded = function() {
646 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
647 if ( document.readyState === "complete" ) {
648 document.detachEvent( "onreadystatechange", DOMContentLoaded );
654 // The DOM ready check for Internet Explorer
655 function doScrollCheck() {
656 if ( jQuery.isReady ) {
661 // If IE is used, use the trick by Diego Perini
662 // http://javascript.nwbox.com/IEContentLoaded/
663 document.documentElement.doScroll("left");
665 setTimeout( doScrollCheck, 1 );
669 // and execute any waiting functions
674 jQuery.inArray = function( elem, array ) {
675 return indexOf.call( array, elem );
679 function evalScript( i, elem ) {
687 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
690 if ( elem.parentNode ) {
691 elem.parentNode.removeChild( elem );
695 // Mutifunctional method to get and set values to a collection
696 // The value/s can be optionally by executed if its a function
697 function access( elems, key, value, exec, fn, pass ) {
698 var length = elems.length;
700 // Setting many attributes
701 if ( typeof key === "object" ) {
702 for ( var k in key ) {
703 access( elems, k, key[k], exec, fn, value );
708 // Setting one attribute
709 if ( value !== undefined ) {
710 // Optionally, function values get executed if exec is true
711 exec = exec && jQuery.isFunction(value);
713 for ( var i = 0; i < length; i++ ) {
714 fn( elems[i], key, exec ? value.call( elems[i], i ) : value, pass );
720 // Getting an attribute
721 return length ? fn( elems[0], key ) : null;
725 return (new Date).getTime();