views:

137

answers:

4

In this video there is a snippet of code that goes something like this:

if (jQuery) {jQuery(function() {
    // ...
})}

I've never seen the jQuery() function before (then again, I'm not a savvy jQuery user), what does it do? Does it ship by default with jQuery or is it specific to IxEdit? Since the usual $(window).load() snippet is missing and the code is somewhat similar I'm guessing it's a shortcut / alias to:

$(window).load(function() {
    // ...
)}

Am I right? Also what is that jQuery variable? What does it hold? And why is he checking it?

+5  A: 

$() is an alias for jQuery(), defined as:

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

http://code.jquery.com/jquery-1.4.js

there is a special case defined when $() or jQuery() is called with the first argument being a function:

// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
    return rootjQuery.ready( selector );
}

sometimes $ can conflict with other libraries (like prototype) that define the same function, so if you call

jQuery.noConflict();

it will remove the $ alias, setting it back to the original value found, essentially:

window.$ = _$;
jspcal
So the first snippet is not checking if the page is already fully loaded? Also, why does he uses the `if (jQuery)` statement?
Alix Axel
yah exactly right.. kind of tricky, window.jquery is set to local var jquery which is a function (var jQuery = function( selector, context ) { ...). if (jQuery) would check if function defined, you can also say if ('jQuery' in window) ... if var not init'd
jspcal
I'm sorry but I'm still a little bit confused, how would you write `$(document).load(function()` and `$(document).ready(function()` using the `jQuery()` function?
Alix Axel
@pulse says `jQuery()` is an alias to `$(document).ready()` not to `$()`.
Alix Axel
jQuery/$ check the type of the first argument, if it's a function, it calls ready()
jspcal
+2  A: 

The $ function is an alias for the jQuery function. So, they are the same.

If you use jQuery in noConflict mode, there is only jQuery() function

valya
+1  A: 

I think it is the same that using $() but you use jQuery() for compatibility with other libs which also use $()

jQuery can be a variable that store a function. Guess that if is to check if it is not undefined or something like that

fmsf
+3  A: 
jQuery(function()

is same as

$(document).ready(function()

if(jQuery)

is a check whether the jQuery.js file has been loaded or not.

There is another way to check this

if (typeof jQuery == 'undefined')
{
    //jQuery has not been loaded  
}
rahul
Thanks! So will `jQuery()` alone also check for the document `ready` status?
Alix Axel