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;
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 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
93 elem = document.getElementById( match[2] );
96 // Handle the case where IE and Opera return items
97 // by name instead of ID
98 if ( elem.id !== match[2] ) {
99 return rootjQuery.find( selector );
102 // Otherwise, we inject the element directly into the jQuery object
107 this.context = document;
108 this.selector = selector;
113 } else if ( !context && /^\w+$/.test( selector ) ) {
114 this.selector = selector;
115 this.context = document;
116 selector = document.getElementsByTagName( selector );
118 // HANDLE: $(expr, $(...))
119 } else if ( !context || context.jquery ) {
120 return (context || rootjQuery).find( selector );
122 // HANDLE: $(expr, context)
123 // (which is just equivalent to: $(context).find(expr)
125 return jQuery( context ).find( selector );
128 // HANDLE: $(function)
129 // Shortcut for document ready
130 } else if ( jQuery.isFunction( selector ) ) {
131 return rootjQuery.ready( selector );
134 if (selector.selector !== undefined) {
135 this.selector = selector.selector;
136 this.context = selector.context;
139 return jQuery.isArray( selector ) ?
140 this.setArray( selector ) :
141 jQuery.makeArray( selector, this );
144 // Start with an empty selector
147 // The current version of jQuery being used
150 // The default length of a jQuery object is 0
153 // The number of elements contained in the matched element set
159 return slice.call( this, 0 );
162 // Get the Nth element in the matched element set OR
163 // Get the whole matched element set as a clean array
164 get: function( num ) {
167 // Return a 'clean' array
170 // Return just the object
171 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
174 // Take an array of elements and push it onto the stack
175 // (returning the new matched element set)
176 pushStack: function( elems, name, selector ) {
177 // Build a new jQuery matched element set
178 var ret = jQuery( elems || null );
180 // Add the old object onto the stack (as a reference)
181 ret.prevObject = this;
183 ret.context = this.context;
185 if ( name === "find" ) {
186 ret.selector = this.selector + (this.selector ? " " : "") + selector;
188 ret.selector = this.selector + "." + name + "(" + selector + ")";
191 // Return the newly-formed element set
195 // Force the current matched set of elements to become
196 // the specified array of elements (destroying the stack in the process)
197 // You should use pushStack() in order to do this, but maintain the stack
198 setArray: function( elems ) {
199 // Resetting the length to 0, then using the native Array push
200 // is a super-fast way to populate an object with array-like properties
202 push.apply( this, elems );
207 // Execute a callback for every element in the matched set.
208 // (You can seed the arguments with an array of args, but this is
209 // only used internally.)
210 each: function( callback, args ) {
211 return jQuery.each( this, callback, args );
214 // Determine the position of an element within
215 // the matched set of elements
216 index: function( elem ) {
217 if ( !elem || typeof elem === "string" ) {
218 return jQuery.inArray( this[0],
219 // If it receives a string, the selector is used
220 // If it receives nothing, the siblings are used
221 elem ? jQuery( elem ) : this.parent().children() );
223 // Locate the position of the desired element
224 return jQuery.inArray(
225 // If it receives a jQuery object, the first element is used
226 elem.jquery ? elem[0] : elem, this );
229 is: function( selector ) {
230 return !!selector && jQuery.filter( selector, this ).length > 0;
233 ready: function( fn ) {
234 // Attach the listeners
237 // If the DOM is already ready
238 if ( jQuery.isReady && !readyList ) {
239 // Execute the function immediately
240 fn.call( document, jQuery );
242 // Otherwise, remember the function for later
244 // Add the function to the wait list
245 readyList.push( fn );
251 // For internal use only.
252 // Behaves like an Array's method, not like a jQuery method.
258 // Give the init function the jQuery prototype for later instantiation
259 jQuery.fn.init.prototype = jQuery.fn;
261 jQuery.extend = jQuery.fn.extend = function() {
262 // copy reference to target object
263 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
265 // Handle a deep copy situation
266 if ( typeof target === "boolean" ) {
268 target = arguments[1] || {};
269 // skip the boolean and the target
273 // Handle case when target is a string or something (possible in deep copy)
274 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
278 // extend jQuery itself if only one argument is passed
279 if ( length === i ) {
284 for ( ; i < length; i++ ) {
285 // Only deal with non-null/undefined values
286 if ( (options = arguments[ i ]) != null ) {
287 // Extend the base object
288 for ( name in options ) {
289 src = target[ name ];
290 copy = options[ name ];
292 // Prevent never-ending loop
293 if ( target === copy ) {
297 // Recurse if we're merging object literal values
298 if ( deep && copy && jQuery.isPlainObject(copy) ) {
299 // Don't extend not object literals
300 var clone = src && jQuery.isPlainObject(src) ? src : {};
302 // Never move original objects, clone them
303 target[ name ] = jQuery.extend( deep, clone, copy );
305 // Don't bring in undefined values
306 } else if ( copy !== undefined ) {
307 target[ name ] = copy;
313 // Return the modified object
318 noConflict: function( deep ) {
322 window.jQuery = _jQuery;
328 // Is the DOM ready to be used? Set to true once it occurs.
331 // Handle when the DOM is ready
333 // Make sure that the DOM is not already loaded
334 if ( !jQuery.isReady ) {
335 if ( !document.body ) {
336 return setTimeout( jQuery.ready, 13 );
339 // Remember that the DOM is ready
340 jQuery.isReady = true;
342 // If there are functions bound, to execute
344 // Execute all of them
346 while ( (fn = readyList[ i++ ]) ) {
347 fn.call( document, jQuery );
350 // Reset the list of functions
354 // Trigger any bound ready events
355 if ( jQuery.fn.triggerHandler ) {
356 jQuery( document ).triggerHandler( "ready" );
361 bindReady: function() {
362 if ( readyBound ) { return; }
365 // Catch cases where $(document).ready() is called after the
366 // browser event has already occurred.
367 if ( document.readyState === "complete" ) {
368 return jQuery.ready();
371 // Mozilla, Opera and webkit nightlies currently support this event
372 if ( document.addEventListener ) {
373 // Use the handy event callback
374 document.addEventListener( "DOMContentLoaded", function DOMContentLoaded() {
375 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
379 // A fallback to window.onload, that will always work
380 window.addEventListener( "load", jQuery.ready, false );
382 // If IE event model is used
383 } else if ( document.attachEvent ) {
384 // ensure firing before onload,
385 // maybe late but safe also for iframes
386 document.attachEvent("onreadystatechange", function onreadystatechange() {
387 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
388 if ( document.readyState === "complete" ) {
389 document.detachEvent( "onreadystatechange", onreadystatechange );
394 // A fallback to window.onload, that will always work
395 window.attachEvent( "onload", jQuery.ready );
397 // If IE and not a frame
398 // continually check to see if the document is ready
399 var toplevel = false;
402 toplevel = window.frameElement == null;
405 if ( document.documentElement.doScroll && toplevel ) {
408 function doScrollCheck() {
409 if ( jQuery.isReady ) {
414 // If IE is used, use the trick by Diego Perini
415 // http://javascript.nwbox.com/IEContentLoaded/
416 document.documentElement.doScroll("left");
418 setTimeout( doScrollCheck, 1 );
422 // and execute any waiting functions
429 // See test/unit/core.js for details concerning isFunction.
430 // Since version 1.3, DOM methods and functions like alert
431 // aren't supported. They return false on IE (#2968).
432 isFunction: function( obj ) {
433 return toString.call(obj) === "[object Function]";
436 isArray: function( obj ) {
437 return toString.call(obj) === "[object Array]";
440 isPlainObject: function( obj ) {
441 if ( toString.call(obj) !== "[object Object]" || typeof obj.nodeType === "number" ) {
445 // not own constructor property must be Object
447 && !hasOwnProperty.call(obj, "constructor")
448 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
452 //own properties are iterated firstly,
453 //so to speed up, we can test last one if it is own or not
456 for ( key in obj ) {}
458 return key === undefined || hasOwnProperty.call( obj, key );
461 isEmptyObject: function( obj ) {
462 for ( var name in obj ) {
468 // Evalulates a script in a global context
469 globalEval: function( data ) {
470 if ( data && rnotwhite.test(data) ) {
471 // Inspired by code by Andrea Giammarchi
472 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
473 var head = document.getElementsByTagName("head")[0] || document.documentElement,
474 script = document.createElement("script");
476 script.type = "text/javascript";
478 if ( jQuery.support.scriptEval ) {
479 script.appendChild( document.createTextNode( data ) );
484 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
485 // This arises when a base node is used (#2709).
486 head.insertBefore( script, head.firstChild );
487 head.removeChild( script );
491 nodeName: function( elem, name ) {
492 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
495 // args is for internal usage only
496 each: function( object, callback, args ) {
498 length = object.length,
499 isObj = length === undefined || jQuery.isFunction(object);
503 for ( name in object ) {
504 if ( callback.apply( object[ name ], args ) === false ) {
509 for ( ; i < length; ) {
510 if ( callback.apply( object[ i++ ], args ) === false ) {
516 // A special, fast, case for the most common use of each
519 for ( name in object ) {
520 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
525 for ( var value = object[0];
526 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
533 trim: function( text ) {
534 return (text || "").replace( rtrim, "" );
537 // results is for internal usage only
538 makeArray: function( array, results ) {
539 var ret = results || [];
541 if ( array != null ) {
542 // The window, strings (and functions) also have 'length'
543 // The extra typeof function check is to prevent crashes
544 // in Safari 2 (See: #3039)
545 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
546 push.call( ret, array );
548 jQuery.merge( ret, array );
555 inArray: function( elem, array ) {
556 if ( array.indexOf ) {
557 return array.indexOf( elem );
560 for ( var i = 0, length = array.length; i < length; i++ ) {
561 if ( array[ i ] === elem ) {
569 merge: function( first, second ) {
570 var pos, i = second.length;
572 // We have to get length this way when IE & Opera overwrite the length
573 // expando of getElementsByTagName
574 if ( i && i.nodeType ) {
575 for ( i = 0; second[i]; ++i ) {}
578 pos = i + first.length;
580 // Correct length for non Arrays
584 first[ --pos ] = second[ --i ];
590 grep: function( elems, callback, inv ) {
593 // Go through the array, only saving the items
594 // that pass the validator function
595 for ( var i = 0, length = elems.length; i < length; i++ ) {
596 if ( !inv !== !callback( elems[ i ], i ) ) {
597 ret.push( elems[ i ] );
604 // arg is for internal usage only
605 map: function( elems, callback, arg ) {
608 // Go through the array, translating each of the items to their
609 // new value (or values).
610 for ( var i = 0, length = elems.length; i < length; i++ ) {
611 value = callback( elems[ i ], i, arg );
613 if ( value != null ) {
614 ret[ ret.length ] = value;
618 return ret.concat.apply( [], ret );
621 // Use of jQuery.browser is frowned upon.
622 // More details: http://docs.jquery.com/Utilities/jQuery.browser
624 version: (/.*?(?:firefox|safari|opera|msie)[\/ ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
625 safari: /safari/.test( userAgent ),
626 opera: /opera/.test( userAgent ),
627 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
628 firefox: /firefox/.test( userAgent )
633 jQuery.browser.mozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
636 jQuery.inArray = function( elem, array ) {
637 return indexOf.call( array, elem );
641 // All jQuery objects should point back to these
642 rootjQuery = jQuery(document);
644 function evalScript( i, elem ) {
652 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
655 if ( elem.parentNode ) {
656 elem.parentNode.removeChild( elem );
660 // Mutifunctional method to get and set values to a collection
661 // The value/s can be optionally by executed if its a function
662 function access( elems, key, value, exec, fn ) {
663 var l = elems.length;
665 // Setting many attributes
666 if ( typeof key === "object" ) {
668 access(elems, k, key[k], exec, fn);
673 // Setting one attribute
674 if (value !== undefined) {
675 // Optionally, function values get executed if exec is true
676 exec = exec && jQuery.isFunction(value);
678 for (var i = 0; i < l; i++) {
680 val = exec ? value.call(elem, i) : value;
686 // Getting an attribute
687 return l ? fn(elems[0], key) : null;
691 return (new Date).getTime();