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 ready: function( fn ) {
215 // Attach the listeners
218 // If the DOM is already ready
219 if ( jQuery.isReady && !readyList ) {
220 // Execute the function immediately
221 fn.call( document, jQuery );
223 // Otherwise, remember the function for later
225 // Add the function to the wait list
226 readyList.push( fn );
235 this.slice( i, +i + 1 );
243 return this.eq( -1 );
247 return this.pushStack( slice.apply( this, arguments ),
248 "slice", slice.call(arguments).join(",") );
251 map: function( callback ) {
252 return this.pushStack( jQuery.map(this, function(elem, i){
253 return callback.call( elem, i, elem );
258 return this.prevObject || jQuery(null);
261 // For internal use only.
262 // Behaves like an Array's method, not like a jQuery method.
268 // Give the init function the jQuery prototype for later instantiation
269 jQuery.fn.init.prototype = jQuery.fn;
271 jQuery.extend = jQuery.fn.extend = function() {
272 // copy reference to target object
273 var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
275 // Handle a deep copy situation
276 if ( typeof target === "boolean" ) {
278 target = arguments[1] || {};
279 // skip the boolean and the target
283 // Handle case when target is a string or something (possible in deep copy)
284 if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
288 // extend jQuery itself if only one argument is passed
289 if ( length === i ) {
294 for ( ; i < length; i++ ) {
295 // Only deal with non-null/undefined values
296 if ( (options = arguments[ i ]) != null ) {
297 // Extend the base object
298 for ( name in options ) {
299 src = target[ name ];
300 copy = options[ name ];
302 // Prevent never-ending loop
303 if ( target === copy ) {
307 // Recurse if we're merging object literal values
308 if ( deep && copy && jQuery.isPlainObject(copy) ) {
309 // Don't extend not object literals
310 var clone = src && jQuery.isPlainObject(src) ? src : {};
312 // Never move original objects, clone them
313 target[ name ] = jQuery.extend( deep, clone, copy );
315 // Don't bring in undefined values
316 } else if ( copy !== undefined ) {
317 target[ name ] = copy;
323 // Return the modified object
328 noConflict: function( deep ) {
332 window.jQuery = _jQuery;
338 // Is the DOM ready to be used? Set to true once it occurs.
341 // Handle when the DOM is ready
343 // Make sure that the DOM is not already loaded
344 if ( !jQuery.isReady ) {
345 if ( !document.body ) {
346 return setTimeout( jQuery.ready, 13 );
349 // Remember that the DOM is ready
350 jQuery.isReady = true;
352 // If there are functions bound, to execute
354 // Execute all of them
356 while ( (fn = readyList[ i++ ]) ) {
357 fn.call( document, jQuery );
360 // Reset the list of functions
364 // Trigger any bound ready events
365 if ( jQuery.fn.triggerHandler ) {
366 jQuery( document ).triggerHandler( "ready" );
371 bindReady: function() {
372 if ( readyBound ) { return; }
375 // Catch cases where $(document).ready() is called after the
376 // browser event has already occurred.
377 if ( document.readyState === "complete" ) {
378 return jQuery.ready();
381 // Mozilla, Opera and webkit nightlies currently support this event
382 if ( document.addEventListener ) {
383 // Use the handy event callback
384 document.addEventListener( "DOMContentLoaded", function DOMContentLoaded() {
385 document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
389 // A fallback to window.onload, that will always work
390 window.addEventListener( "load", jQuery.ready, false );
392 // If IE event model is used
393 } else if ( document.attachEvent ) {
394 // ensure firing before onload,
395 // maybe late but safe also for iframes
396 document.attachEvent("onreadystatechange", function onreadystatechange() {
397 // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
398 if ( document.readyState === "complete" ) {
399 document.detachEvent( "onreadystatechange", onreadystatechange );
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 ) {
418 function doScrollCheck() {
419 if ( jQuery.isReady ) {
424 // If IE is used, use the trick by Diego Perini
425 // http://javascript.nwbox.com/IEContentLoaded/
426 document.documentElement.doScroll("left");
428 setTimeout( doScrollCheck, 1 );
432 // and execute any waiting functions
439 // See test/unit/core.js for details concerning isFunction.
440 // Since version 1.3, DOM methods and functions like alert
441 // aren't supported. They return false on IE (#2968).
442 isFunction: function( obj ) {
443 return toString.call(obj) === "[object Function]";
446 isArray: function( obj ) {
447 return toString.call(obj) === "[object Array]";
450 isPlainObject: function( obj ) {
451 if ( toString.call(obj) !== "[object Object]" || typeof obj.nodeType === "number" ) {
455 // not own constructor property must be Object
457 && !hasOwnProperty.call(obj, "constructor")
458 && !hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf") ) {
462 //own properties are iterated firstly,
463 //so to speed up, we can test last one if it is own or not
466 for ( key in obj ) {}
468 return key === undefined || hasOwnProperty.call( obj, key );
471 isEmptyObject: function( obj ) {
472 for ( var name in obj ) {
478 // Evalulates a script in a global context
479 globalEval: function( data ) {
480 if ( data && rnotwhite.test(data) ) {
481 // Inspired by code by Andrea Giammarchi
482 // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
483 var head = document.getElementsByTagName("head")[0] || document.documentElement,
484 script = document.createElement("script");
486 script.type = "text/javascript";
488 if ( jQuery.support.scriptEval ) {
489 script.appendChild( document.createTextNode( data ) );
494 // Use insertBefore instead of appendChild to circumvent an IE6 bug.
495 // This arises when a base node is used (#2709).
496 head.insertBefore( script, head.firstChild );
497 head.removeChild( script );
501 nodeName: function( elem, name ) {
502 return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
505 // args is for internal usage only
506 each: function( object, callback, args ) {
508 length = object.length,
509 isObj = length === undefined || jQuery.isFunction(object);
513 for ( name in object ) {
514 if ( callback.apply( object[ name ], args ) === false ) {
519 for ( ; i < length; ) {
520 if ( callback.apply( object[ i++ ], args ) === false ) {
526 // A special, fast, case for the most common use of each
529 for ( name in object ) {
530 if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
535 for ( var value = object[0];
536 i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
543 trim: function( text ) {
544 return (text || "").replace( rtrim, "" );
547 // results is for internal usage only
548 makeArray: function( array, results ) {
549 var ret = results || [];
551 if ( array != null ) {
552 // The window, strings (and functions) also have 'length'
553 // The extra typeof function check is to prevent crashes
554 // in Safari 2 (See: #3039)
555 if ( array.length == null || typeof array === "string" || jQuery.isFunction(array) || (typeof array !== "function" && array.setInterval) ) {
556 push.call( ret, array );
558 jQuery.merge( ret, array );
565 inArray: function( elem, array ) {
566 if ( array.indexOf ) {
567 return array.indexOf( elem );
570 for ( var i = 0, length = array.length; i < length; i++ ) {
571 if ( array[ i ] === elem ) {
579 merge: function( first, second ) {
580 var i = first.length, j = 0;
582 if ( typeof second.length === "number" ) {
583 for ( var l = second.length; j < l; j++ ) {
584 first[ i++ ] = second[ j ];
587 while ( second[j] !== undefined ) {
588 first[ i++ ] = second[ j++ ];
597 grep: function( elems, callback, inv ) {
600 // Go through the array, only saving the items
601 // that pass the validator function
602 for ( var i = 0, length = elems.length; i < length; i++ ) {
603 if ( !inv !== !callback( elems[ i ], i ) ) {
604 ret.push( elems[ i ] );
611 // arg is for internal usage only
612 map: function( elems, callback, arg ) {
615 // Go through the array, translating each of the items to their
616 // new value (or values).
617 for ( var i = 0, length = elems.length; i < length; i++ ) {
618 value = callback( elems[ i ], i, arg );
620 if ( value != null ) {
621 ret[ ret.length ] = value;
625 return ret.concat.apply( [], ret );
628 // Use of jQuery.browser is frowned upon.
629 // More details: http://docs.jquery.com/Utilities/jQuery.browser
631 version: (/.*?(?:firefox|safari|opera|msie)[\/ ]([\d.]+)/.exec(userAgent) || [0,'0'])[1],
632 safari: /safari/.test( userAgent ),
633 opera: /opera/.test( userAgent ),
634 msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
635 firefox: /firefox/.test( userAgent )
640 jQuery.browser.mozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
643 jQuery.inArray = function( elem, array ) {
644 return indexOf.call( array, elem );
648 // All jQuery objects should point back to these
649 rootjQuery = jQuery(document);
651 function evalScript( i, elem ) {
659 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
662 if ( elem.parentNode ) {
663 elem.parentNode.removeChild( elem );
667 // Mutifunctional method to get and set values to a collection
668 // The value/s can be optionally by executed if its a function
669 function access( elems, key, value, exec, fn ) {
670 var length = elems.length;
672 // Setting many attributes
673 if ( typeof key === "object" ) {
674 for ( var k in key ) {
675 access( elems, k, key[k], exec, fn );
680 // Setting one attribute
681 if ( value !== undefined ) {
682 // Optionally, function values get executed if exec is true
683 exec = exec && jQuery.isFunction(value);
685 for ( var i = 0; i < length; i++ ) {
686 fn( elems[i], key, exec ? value.call( elems[i], i ) : value );
692 // Getting an attribute
693 return length ? fn( elems[0], key ) : null;
697 return (new Date).getTime();