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 // Save a reference to some core methods
45 toString = Object.prototype.toString,
46 hasOwnProperty = Object.prototype.hasOwnProperty,
47 push = Array.prototype.push,
48 slice = Array.prototype.slice,
49 indexOf = Array.prototype.indexOf;
51 jQuery.fn = jQuery.prototype = {
52 init: function( selector, context ) {
53 var match, elem, ret, doc, parent;
55 // Handle $(""), $(null), or $(undefined)
60 // Handle $(DOMElement)
61 if ( selector.nodeType ) {
62 this.context = this[0] = selector;
67 // Handle HTML strings
68 if ( typeof selector === "string" ) {
69 // Are we dealing with HTML string or an ID?
70 match = quickExpr.exec( selector );
72 // Verify a match, and that no context was specified for #id
73 if ( match && (match[1] || !context) ) {
75 // HANDLE: $(html) -> $(array)
77 doc = (context ? context.ownerDocument || context : document);
79 // If a single string is passed in and it's a single tag
80 // just do a createElement and skip the rest
81 ret = rsingleTag.exec( selector );
84 selector = [ doc.createElement( ret[1] ) ];
87 ret = buildFragment( [ match[1] ], [ doc ] );
88 parent = ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment;
91 while ( parent.firstChild ) {
92 selector.push( parent.removeChild( parent.firstChild ) );
98 elem = document.getElementById( match[2] );
101 // Handle the case where IE and Opera return items
102 // by name instead of ID
103 if ( elem.id !== match[2] ) {
104 return rootjQuery.find( selector );
107 // Otherwise, we inject the element directly into the jQuery object
112 this.context = document;
113 this.selector = selector;
118 } else if ( !context && /^\w+$/.test( selector ) ) {
119 this.selector = selector;
120 this.context = document;
121 selector = document.getElementsByTagName( selector );
123 // HANDLE: $(expr, $(...))
124 } else if ( !context || context.jquery ) {
125 return (context || rootjQuery).find( selector );
127 // HANDLE: $(expr, context)
128 // (which is just equivalent to: $(context).find(expr)
130 return jQuery( context ).find( selector );
133 // HANDLE: $(function)
134 // Shortcut for document ready
135 } else if ( jQuery.isFunction( selector ) ) {
136 return rootjQuery.ready( selector );
139 if (selector.selector !== undefined) {
140 this.selector = selector.selector;
141 this.context = selector.context;
144 return jQuery.isArray( selector ) ?
145 this.setArray( selector ) :
146 jQuery.makeArray( selector, this );
149 // Start with an empty selector
152 // The current version of jQuery being used
155 // The default length of a jQuery object is 0
158 // The number of elements contained in the matched element set
164 return slice.call( this, 0 );
167 // Get the Nth element in the matched element set OR
168 // Get the whole matched element set as a clean array
169 get: function( num ) {
172 // Return a 'clean' array
175 // Return just the object
176 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
179 // Take an array of elements and push it onto the stack
180 // (returning the new matched element set)
181 pushStack: function( elems, name, selector ) {
182 // Build a new jQuery matched element set
183 var ret = jQuery( elems || null );
185 // Add the old object onto the stack (as a reference)
186 ret.prevObject = this;
188 ret.context = this.context;
190 if ( name === "find" ) {
191 ret.selector = this.selector + (this.selector ? " " : "") + selector;
193 ret.selector = this.selector + "." + name + "(" + selector + ")";
196 // Return the newly-formed element set
200 // Force the current matched set of elements to become
201 // the specified array of elements (destroying the stack in the process)
202 // You should use pushStack() in order to do this, but maintain the stack
203 setArray: function( elems ) {
204 // Resetting the length to 0, then using the native Array push
205 // is a super-fast way to populate an object with array-like properties
207 push.apply( this, elems );
212 // Execute a callback for every element in the matched set.
213 // (You can seed the arguments with an array of args, but this is
214 // only used internally.)
215 each: function( callback, args ) {
216 return jQuery.each( this, callback, args );
219 ready: function( fn ) {
220 // Attach the listeners
223 // If the DOM is already ready
224 if ( jQuery.isReady && !readyList ) {
225 // Execute the function immediately
226 fn.call( document, jQuery );
228 // Otherwise, remember the function for later
230 // Add the function to the wait list
231 readyList.push( fn );
240 this.slice( i, +i + 1 );
248 return this.eq( -1 );
252 return this.pushStack( slice.apply( this, arguments ),
253 "slice", slice.call(arguments).join(",") );
256 map: function( callback ) {
257 return this.pushStack( jQuery.map(this, function(elem, i){
258 return callback.call( elem, i, elem );
263 return this.prevObject || jQuery(null);
266 // For internal use only.
267 // Behaves like an Array's method, not like a jQuery method.
273 // Give the init function the jQuery prototype for later instantiation
274 jQuery.fn.init.prototype = jQuery.fn;
276 jQuery.extend = jQuery.fn.extend = function() {
277 // copy reference to target object
278 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
280 // Handle a deep copy situation
281 if ( typeof target === "boolean" ) {
283 target = arguments[1] || {};
284 // skip the boolean and the target
288 // Handle case when target is a string or something (possible in deep copy)
289 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
293 // extend jQuery itself if only one argument is passed
294 if ( length === i ) {
299 for ( ; i < length; i++ ) {
300 // Only deal with non-null/undefined values
301 if ( (options = arguments[ i ]) != null ) {
302 // Extend the base object
303 for ( name in options ) {
304 src = target[ name ];
305 copy = options[ name ];
307 // Prevent never-ending loop
308 if ( target === copy ) {
312 // Recurse if we're merging object literal values
313 if ( deep && copy && jQuery.isPlainObject(copy) ) {
314 // Don't extend not object literals
315 var clone = src && jQuery.isPlainObject(src) ? src : {};
317 // Never move original objects, clone them
318 target[ name ] = jQuery.extend( deep, clone, copy );
320 // Don't bring in undefined values
321 } else if ( copy !== undefined ) {
322 target[ name ] = copy;
328 // Return the modified object
333 noConflict: function( deep ) {
337 window.jQuery = _jQuery;
343 // Is the DOM ready to be used? Set to true once it occurs.
346 // Handle when the DOM is ready
348 // Make sure that the DOM is not already loaded
349 if ( !jQuery.isReady ) {
350 if ( !document.body ) {
351 return setTimeout( jQuery.ready, 13 );
354 // Remember that the DOM is ready
355 jQuery.isReady = true;
357 // If there are functions bound, to execute
359 // Execute all of them
361 while ( (fn = readyList[ i++ ]) ) {
362 fn.call( document, jQuery );
365 // Reset the list of functions
369 // Trigger any bound ready events
370 if ( jQuery.fn.triggerHandler ) {
371 jQuery( document ).triggerHandler( "ready" );
376 bindReady: function() {
377 if ( readyBound ) { return; }
380 // Catch cases where $(document).ready() is called after the
381 // browser event has already occurred.
382 if ( document.readyState === "complete" ) {
383 return jQuery.ready();
386 // Mozilla, Opera and webkit nightlies currently support this event
387 if ( document.addEventListener ) {
388 // Use the handy event callback
389 document.addEventListener( "DOMContentLoaded", function DOMContentLoaded() {
390 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
394 // A fallback to window.onload, that will always work
395 window.addEventListener( "load", jQuery.ready, false );
397 // If IE event model is used
398 } else if ( document.attachEvent ) {
399 // ensure firing before onload,
400 // maybe late but safe also for iframes
401 document.attachEvent("onreadystatechange", function onreadystatechange() {
402 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
403 if ( document.readyState === "complete" ) {
404 document.detachEvent( "onreadystatechange", onreadystatechange );
409 // A fallback to window.onload, that will always work
410 window.attachEvent( "onload", jQuery.ready );
412 // If IE and not a frame
413 // continually check to see if the document is ready
414 var toplevel = false;
417 toplevel = window.frameElement == null;
420 if ( document.documentElement.doScroll && toplevel ) {
423 function doScrollCheck() {
424 if ( jQuery.isReady ) {
429 // If IE is used, use the trick by Diego Perini
430 // http://javascript.nwbox.com/IEContentLoaded/
431 document.documentElement.doScroll("left");
433 setTimeout( doScrollCheck, 1 );
437 // and execute any waiting functions
444 // See test/unit/core.js for details concerning isFunction.
445 // Since version 1.3, DOM methods and functions like alert
446 // aren't supported. They return false on IE (#2968).
447 isFunction: function( obj ) {
448 return toString.call(obj) === "[object Function]";
451 isArray: function( obj ) {
452 return toString.call(obj) === "[object Array]";
455 isPlainObject: function( obj ) {
456 if ( toString.call(obj) !== "[object Object]" || typeof obj.nodeType === "number" ) {
460 // not own constructor property must be Object
462 && !hasOwnProperty.call(obj, "constructor")
463 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
467 //own properties are iterated firstly,
468 //so to speed up, we can test last one if it is own or not
471 for ( key in obj ) {}
473 return key === undefined || hasOwnProperty.call( obj, key );
476 isEmptyObject: function( obj ) {
477 for ( var name in obj ) {
483 // Evalulates a script in a global context
484 globalEval: function( data ) {
485 if ( data && rnotwhite.test(data) ) {
486 // Inspired by code by Andrea Giammarchi
487 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
488 var head = document.getElementsByTagName("head")[0] || document.documentElement,
489 script = document.createElement("script");
491 script.type = "text/javascript";
493 if ( jQuery.support.scriptEval ) {
494 script.appendChild( document.createTextNode( data ) );
499 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
500 // This arises when a base node is used (#2709).
501 head.insertBefore( script, head.firstChild );
502 head.removeChild( script );
506 nodeName: function( elem, name ) {
507 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
510 // args is for internal usage only
511 each: function( object, callback, args ) {
513 length = object.length,
514 isObj = length === undefined || jQuery.isFunction(object);
518 for ( name in object ) {
519 if ( callback.apply( object[ name ], args ) === false ) {
524 for ( ; i < length; ) {
525 if ( callback.apply( object[ i++ ], args ) === false ) {
531 // A special, fast, case for the most common use of each
534 for ( name in object ) {
535 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
540 for ( var value = object[0];
541 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
548 trim: function( text ) {
549 return (text || "").replace( rtrim, "" );
552 // results is for internal usage only
553 makeArray: function( array, results ) {
554 var ret = results || [];
556 if ( array != null ) {
557 // The window, strings (and functions) also have 'length'
558 // The extra typeof function check is to prevent crashes
559 // in Safari 2 (See: #3039)
560 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
561 push.call( ret, array );
563 jQuery.merge( ret, array );
570 inArray: function( elem, array ) {
571 if ( array.indexOf ) {
572 return array.indexOf( elem );
575 for ( var i = 0, length = array.length; i < length; i++ ) {
576 if ( array[ i ] === elem ) {
584 merge: function( first, second ) {
585 var i = first.length, j = 0;
587 if ( typeof second.length === "number" ) {
588 for ( var l = second.length; j < l; j++ ) {
589 first[ i++ ] = second[ j ];
592 while ( second[j] !== undefined ) {
593 first[ i++ ] = second[ j++ ];
602 grep: function( elems, callback, inv ) {
605 // Go through the array, only saving the items
606 // that pass the validator function
607 for ( var i = 0, length = elems.length; i < length; i++ ) {
608 if ( !inv !== !callback( elems[ i ], i ) ) {
609 ret.push( elems[ i ] );
616 // arg is for internal usage only
617 map: function( elems, callback, arg ) {
620 // Go through the array, translating each of the items to their
621 // new value (or values).
622 for ( var i = 0, length = elems.length; i < length; i++ ) {
623 value = callback( elems[ i ], i, arg );
625 if ( value != null ) {
626 ret[ ret.length ] = value;
630 return ret.concat.apply( [], ret );
633 // Use of jQuery.browser is frowned upon.
634 // More details: http://docs.jquery.com/Utilities/jQuery.browser
636 version: (/.*?(?:firefox|safari|opera|msie)[\/ ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
637 safari: /safari/.test( userAgent ),
638 opera: /opera/.test( userAgent ),
639 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
640 firefox: /firefox/.test( userAgent )
645 jQuery.browser.mozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
648 jQuery.inArray = function( elem, array ) {
649 return indexOf.call( array, elem );
653 // All jQuery objects should point back to these
654 rootjQuery = jQuery(document);
656 function evalScript( i, elem ) {
664 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
667 if ( elem.parentNode ) {
668 elem.parentNode.removeChild( elem );
672 // Mutifunctional method to get and set values to a collection
673 // The value/s can be optionally by executed if its a function
674 function access( elems, key, value, exec, fn ) {
675 var length = elems.length;
677 // Setting many attributes
678 if ( typeof key === "object" ) {
679 for ( var k in key ) {
680 access( elems, k, key[k], exec, fn );
685 // Setting one attribute
686 if ( value !== undefined ) {
687 // Optionally, function values get executed if exec is true
688 exec = exec && jQuery.isFunction(value);
690 for ( var i = 0; i < length; i++ ) {
691 fn( elems[i], key, exec ? value.call( elems[i], i ) : value );
697 // Getting an attribute
698 return length ? fn( elems[0], key ) : null;
702 return (new Date).getTime();