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 arguments.length === 0 ?
6 new jQuery.fn.init( selector, context );
9 // Map over jQuery in case of overwrite
10 _jQuery = window.jQuery,
12 // Map over the $ in case of overwrite
15 // Use the correct document accordingly with window argument (sandbox)
16 document = window.document,
18 // A central reference to the root jQuery(document)
21 // A simple way to check for HTML strings or ID strings
22 // (both of which we optimize for)
23 quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w-]+)$/,
25 // Is it a simple selector
26 isSimple = /^.[^:#\[\.,]*$/,
28 // Check if a string has a non-whitespace character in it
31 // Used for trimming whitespace
32 rtrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g,
34 // Match a standalone tag
35 rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
37 // Keep a UserAgent string for use with jQuery.browser
38 userAgent = navigator.userAgent.toLowerCase(),
40 // Has the ready events already been bound?
43 // The functions to execute on DOM ready
46 // Save a reference to some core methods
47 toString = Object.prototype.toString,
48 hasOwnProperty = Object.prototype.hasOwnProperty,
49 push = Array.prototype.push,
50 slice = Array.prototype.slice,
51 indexOf = Array.prototype.indexOf;
53 jQuery.fn = jQuery.prototype = {
54 init: function( selector, context ) {
55 var match, elem, ret, doc;
57 // Handle $(""), $(null), or $(undefined)
62 // Handle $(DOMElement)
63 if ( selector.nodeType ) {
64 this.context = this[0] = selector;
69 // Handle HTML strings
70 if ( typeof selector === "string" ) {
71 // Are we dealing with HTML string or an ID?
72 match = quickExpr.exec( selector );
74 // Verify a match, and that no context was specified for #id
75 if ( match && (match[1] || !context) ) {
77 // HANDLE: $(html) -> $(array)
79 doc = (context ? context.ownerDocument || context : document);
81 // If a single string is passed in and it's a single tag
82 // just do a createElement and skip the rest
83 ret = rsingleTag.exec( selector );
86 selector = [ doc.createElement( ret[1] ) ];
89 ret = buildFragment( [ match[1] ], [ doc ] );
90 selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
95 elem = document.getElementById( match[2] );
98 // Handle the case where IE and Opera return items
99 // by name instead of ID
100 if ( elem.id !== match[2] ) {
101 return rootjQuery.find( selector );
104 // Otherwise, we inject the element directly into the jQuery object
109 this.context = document;
110 this.selector = selector;
115 } else if ( !context && /^\w+$/.test( selector ) ) {
116 this.selector = selector;
117 this.context = document;
118 selector = document.getElementsByTagName( selector );
120 // HANDLE: $(expr, $(...))
121 } else if ( !context || context.jquery ) {
122 return (context || rootjQuery).find( selector );
124 // HANDLE: $(expr, context)
125 // (which is just equivalent to: $(context).find(expr)
127 return jQuery( context ).find( selector );
130 // HANDLE: $(function)
131 // Shortcut for document ready
132 } else if ( jQuery.isFunction( selector ) ) {
133 return rootjQuery.ready( selector );
136 if (selector.selector !== undefined) {
137 this.selector = selector.selector;
138 this.context = selector.context;
141 return jQuery.isArray( selector ) ?
142 this.setArray( selector ) :
143 jQuery.makeArray( selector, this );
146 // Start with an empty selector
149 // The current version of jQuery being used
152 // The default length of a jQuery object is 0
155 // The number of elements contained in the matched element set
161 return slice.call( this, 0 );
164 // Get the Nth element in the matched element set OR
165 // Get the whole matched element set as a clean array
166 get: function( num ) {
169 // Return a 'clean' array
172 // Return just the object
173 ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] );
176 // Take an array of elements and push it onto the stack
177 // (returning the new matched element set)
178 pushStack: function( elems, name, selector ) {
179 // Build a new jQuery matched element set
180 var ret = jQuery( elems || null );
182 // Add the old object onto the stack (as a reference)
183 ret.prevObject = this;
185 ret.context = this.context;
187 if ( name === "find" ) {
188 ret.selector = this.selector + (this.selector ? " " : "") + selector;
190 ret.selector = this.selector + "." + name + "(" + selector + ")";
193 // Return the newly-formed element set
197 // Force the current matched set of elements to become
198 // the specified array of elements (destroying the stack in the process)
199 // You should use pushStack() in order to do this, but maintain the stack
200 setArray: function( elems ) {
201 // Resetting the length to 0, then using the native Array push
202 // is a super-fast way to populate an object with array-like properties
204 push.apply( this, elems );
209 // Execute a callback for every element in the matched set.
210 // (You can seed the arguments with an array of args, but this is
211 // only used internally.)
212 each: function( callback, args ) {
213 return jQuery.each( this, callback, args );
216 // Determine the position of an element within
217 // the matched set of elements
218 index: function( elem ) {
219 if ( !elem || typeof elem === "string" ) {
220 return jQuery.inArray( this[0],
221 // If it receives a string, the selector is used
222 // If it receives nothing, the siblings are used
223 elem ? jQuery( elem ) : this.parent().children() );
225 // Locate the position of the desired element
226 return jQuery.inArray(
227 // If it receives a jQuery object, the first element is used
228 elem.jquery ? elem[0] : elem, this );
231 is: function( selector ) {
232 return !!selector && jQuery.filter( selector, this ).length > 0;
235 ready: function( fn ) {
236 // Attach the listeners
239 // If the DOM is already ready
240 if ( jQuery.isReady ) {
241 // Execute the function immediately
242 fn.call( document, jQuery );
244 // Otherwise, remember the function for later
246 // Add the function to the wait list
247 readyList.push( fn );
253 // For internal use only.
254 // Behaves like an Array's method, not like a jQuery method.
260 // Give the init function the jQuery prototype for later instantiation
261 jQuery.fn.init.prototype = jQuery.fn;
263 jQuery.extend = jQuery.fn.extend = function() {
264 // copy reference to target object
265 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
267 // Handle a deep copy situation
268 if ( typeof target === "boolean" ) {
270 target = arguments[1] || {};
271 // skip the boolean and the target
275 // Handle case when target is a string or something (possible in deep copy)
276 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
280 // extend jQuery itself if only one argument is passed
281 if ( length === i ) {
286 for ( ; i < length; i++ ) {
287 // Only deal with non-null/undefined values
288 if ( (options = arguments[ i ]) != null ) {
289 // Extend the base object
290 for ( name in options ) {
291 src = target[ name ];
292 copy = options[ name ];
294 // Prevent never-ending loop
295 if ( target === copy ) {
299 // Recurse if we're merging object literal values
300 if ( deep && copy && jQuery.isPlainObject(copy) ) {
301 // Don't extend not object literals
302 var clone = src && jQuery.isPlainObject(src) ? src : {};
304 // Never move original objects, clone them
305 target[ name ] = jQuery.extend( deep, clone, copy );
307 // Don't bring in undefined values
308 } else if ( copy !== undefined ) {
309 target[ name ] = copy;
315 // Return the modified object
320 noConflict: function( deep ) {
324 window.jQuery = _jQuery;
330 // Is the DOM ready to be used? Set to true once it occurs.
333 // Handle when the DOM is ready
335 // Make sure that the DOM is not already loaded
336 if ( !jQuery.isReady ) {
337 if ( !document.body ) {
338 return setTimeout( jQuery.ready, 13 );
341 // Remember that the DOM is ready
342 jQuery.isReady = true;
344 // If there are functions bound, to execute
346 // Execute all of them
348 while ( (fn = readyList[ i++ ]) ) {
349 fn.call( document, jQuery );
352 // Reset the list of functions
356 // Trigger any bound ready events
357 if ( jQuery.fn.triggerHandler ) {
358 jQuery( document ).triggerHandler( "ready" );
363 bindReady: function() {
364 if ( readyBound ) { return; }
367 // Catch cases where $(document).ready() is called after the
368 // browser event has already occurred.
369 if ( document.readyState === "complete" ) {
370 return jQuery.ready();
373 // Mozilla, Opera and webkit nightlies currently support this event
374 if ( document.addEventListener ) {
375 // Use the handy event callback
376 document.addEventListener( "DOMContentLoaded", function DOMContentLoaded() {
377 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
381 // A fallback to window.onload, that will always work
382 window.addEventListener( "load", jQuery.ready, false );
384 // If IE event model is used
385 } else if ( document.attachEvent ) {
386 // ensure firing before onload,
387 // maybe late but safe also for iframes
388 document.attachEvent("onreadystatechange", function onreadystatechange() {
389 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
390 if ( document.readyState === "complete" ) {
391 document.detachEvent( "onreadystatechange", onreadystatechange );
396 // A fallback to window.onload, that will always work
397 window.attachEvent( "onload", jQuery.ready );
399 // If IE and not a frame
400 // continually check to see if the document is ready
401 var toplevel = false;
404 toplevel = window.frameElement == null;
407 if ( document.documentElement.doScroll && toplevel ) {
410 function doScrollCheck() {
411 if ( jQuery.isReady ) {
416 // If IE is used, use the trick by Diego Perini
417 // http://javascript.nwbox.com/IEContentLoaded/
418 document.documentElement.doScroll("left");
420 setTimeout( doScrollCheck, 1 );
424 // and execute any waiting functions
431 // See test/unit/core.js for details concerning isFunction.
432 // Since version 1.3, DOM methods and functions like alert
433 // aren't supported. They return false on IE (#2968).
434 isFunction: function( obj ) {
435 return toString.call(obj) === "[object Function]";
438 isArray: function( obj ) {
439 return toString.call(obj) === "[object Array]";
442 isPlainObject: function( obj ) {
443 if ( toString.call(obj) !== "[object Object]" || typeof obj.nodeType === "number" ) {
447 // not own constructor property must be Object
449 && !hasOwnProperty.call(obj, "constructor")
450 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
454 //own properties are iterated firstly,
455 //so to speed up, we can test last one if it is own or not
458 for ( key in obj ) {}
460 return key === undefined || hasOwnProperty.call( obj, key );
463 isEmptyObject: function( obj ) {
464 for ( var name in obj ) {
470 // Evalulates a script in a global context
471 globalEval: function( data ) {
472 if ( data && rnotwhite.test(data) ) {
473 // Inspired by code by Andrea Giammarchi
474 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
475 var head = document.getElementsByTagName("head")[0] || document.documentElement,
476 script = document.createElement("script");
478 script.type = "text/javascript";
480 if ( jQuery.support.scriptEval ) {
481 script.appendChild( document.createTextNode( data ) );
486 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
487 // This arises when a base node is used (#2709).
488 head.insertBefore( script, head.firstChild );
489 head.removeChild( script );
493 nodeName: function( elem, name ) {
494 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
497 // args is for internal usage only
498 each: function( object, callback, args ) {
500 length = object.length,
501 isObj = length === undefined || jQuery.isFunction(object);
505 for ( name in object ) {
506 if ( callback.apply( object[ name ], args ) === false ) {
511 for ( ; i < length; ) {
512 if ( callback.apply( object[ i++ ], args ) === false ) {
518 // A special, fast, case for the most common use of each
521 for ( name in object ) {
522 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
527 for ( var value = object[0];
528 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
535 trim: function( text ) {
536 return (text || "").replace( rtrim, "" );
539 // results is for internal usage only
540 makeArray: function( array, results ) {
541 var ret = results || [];
543 if ( array != null ) {
544 // The window, strings (and functions) also have 'length'
545 // The extra typeof function check is to prevent crashes
546 // in Safari 2 (See: #3039)
547 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
548 push.call( ret, array );
550 jQuery.merge( ret, array );
557 inArray: function( elem, array ) {
558 if ( array.indexOf ) {
559 return array.indexOf( elem );
562 for ( var i = 0, length = array.length; i < length; i++ ) {
563 if ( array[ i ] === elem ) {
571 merge: function( first, second ) {
572 var pos, i = second.length;
574 // We have to get length this way when IE & Opera overwrite the length
575 // expando of getElementsByTagName
576 if ( i && i.nodeType ) {
577 for ( i = 0; second[i]; ++i ) {}
580 pos = i + first.length;
582 // Correct length for non Arrays
586 first[ --pos ] = second[ --i ];
592 grep: function( elems, callback, inv ) {
595 // Go through the array, only saving the items
596 // that pass the validator function
597 for ( var i = 0, length = elems.length; i < length; i++ ) {
598 if ( !inv !== !callback( elems[ i ], i ) ) {
599 ret.push( elems[ i ] );
606 // arg is for internal usage only
607 map: function( elems, callback, arg ) {
610 // Go through the array, translating each of the items to their
611 // new value (or values).
612 for ( var i = 0, length = elems.length; i < length; i++ ) {
613 value = callback( elems[ i ], i, arg );
615 if ( value != null ) {
616 ret[ ret.length ] = value;
620 return ret.concat.apply( [], ret );
623 // Use of jQuery.browser is frowned upon.
624 // It's included for backwards compatibility and plugins,
625 // although they should work to migrate away.
627 version: (/.*?(?:firefox|safari|opera|msie)[\/ ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
628 safari: /safari/.test( userAgent ),
629 opera: /opera/.test( userAgent ),
630 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
631 firefox: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
636 jQuery.browser.mozilla = jQuery.browser.firefox;
639 jQuery.inArray = function( elem, array ) {
640 return indexOf.call( array, elem );
644 // All jQuery objects should point back to these
645 rootjQuery = jQuery(document);
647 function evalScript( i, elem ) {
655 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
658 if ( elem.parentNode ) {
659 elem.parentNode.removeChild( elem );
663 // Mutifunctional method to get and set values to a collection
664 // The value/s can be optionally by executed if its a function
665 function access( elems, key, value, exec, fn ) {
666 var l = elems.length;
668 // Setting many attributes
669 if ( typeof key === "object" ) {
671 access(elems, k, key[k], exec, fn);
676 // Setting one attribute
677 if (value !== undefined) {
678 // Optionally, function values get executed if exec is true
679 exec = exec && jQuery.isFunction(value);
681 for (var i = 0; i < l; i++) {
683 val = exec ? value.call(elem, i) : value;
689 // Getting an attribute
690 return l ? fn(elems[0], key) : null;
694 return (new Date).getTime();