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?
$() 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.$ = _$;
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
}
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
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
Related
I've seen four different ways to tell jQuery to execute a function when the document is ready. Are these all equivalent?
$(document).ready(function () {
alert('$(document).ready()');
});
$().ready(function () {
alert('$().ready()');
});
$(function () {
alert('$()');
});
jQuery(function ($) {
alert('jQuery()');
});
There is no difference.
$ is the same as jQuery. If you view the unminified source, you will see var $ = jQuery = ... or something to that effect.
The jQuery function checks the type of it's parameter, if it is a function, it treats it the same as $(document).ready(...)
Calling jQuery without a parameter defaults to using document. So $() and $(document) are identical. Try it in Firebug.
re: Geroge IV's comments regarding $() == $(document) its correct. From the unminified source (init is what get called internally):
init: function( selector, context ) {
// Make sure that a selection was provided
selector = selector || document;
Also from source, to back up previous conversations:
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) )
return jQuery( document ).ready( selector );
this should be community wiki. I've always been interested in the inner workings of jquery, now I've had an excuse to start looking :-)
Also it should be mentioned, that symbol that you pass to function will be use inside the function. For example:
$(function(jQuery) {
// now I can use jQuery instead $
jQuery("body").append("<div></div>"); // adds div to the end of body element
});
if you want use $ - you can leave function's param in this situation empty
The real example you can find here http://jsfiddle.net/yura_syedin/BNgd4/
Here's another one - starts like this...
(function (jQuery) {
then to finish...
})(jQuery);
An example is here:
http://jsfiddle.net/C2qZw/23/
I am using selenium addon as well as jquery in my addon. Due to use of jquery functions with $ being used in selenium throwing function not found error. Removing the Jquery, everything works fine. Using Jquery (ajax call) is must for me. Please suggest how can I make them work together.
One recommended way to solve this kind of conflict is to wrap your javascript code inside a function, and pass jQuery as an argument to this function :
// e.g : turn this code :
$(function(){
$('.my-class').on('click', function(){
$.ajax(...);
});
...
});
// into :
(function($) { // start an anonymous function,
// whose first argument is named '$' ...
$(function(){
$('.my-class').on('click', function(){
$.ajax(...);
});
...
});
}(jQuery)); // and call this function right away,
// passing the jQuery object as first argument
Note that $ is just a shortcut for jQuery :
jQuery('.my-class') and jQuery.ajax(...)
// are exactly the same as :
$('.my-class') and jQuery.ajax(...)
You can also use your own alias :
var $j = jQuery;
If some day you need to mix jQuery with another library which defines a $ variable, you can also use jQuery.noConflict() (example taken from this use case) :
var $j = jQuery.noConflict();
When using Jquery a syntax error occured. What is the difference between the two
$.ajax({
//working
});
Jquery.ajax({
//not work
});
$ is just an alias/shortcut for the formal name jQuery. This was done by jQuery to reduce the weight of scripts that would depend on the library.
you have misspelled the library identifier in your example:
//Your Example
JQuery.ajax({
});
//What it should look like
jQuery.ajax({
});
The 2nd code above should work fine like
$.ajax({
});
if in case the dollar sign ($) is also used by other libraries. You can use the jQuery.noConflict(); to give way to other libaries.
The correct name is jQuery, not Jquery. jQuery and $ are the same thing. $ is just a shortcut. Please read the documentation to get more information about how to properly use jQuery - https://api.jquery.com/
As the others already point out the problem:-
jQuery, not Jquery .
The below code from jQuery Source Code will show you how jQuery sets $ as alias or shortcut. And what happens when jQuery.noConflict is called.
http://code.jquery.com/jquery-2.1.4.js
var
// Map over jQuery in case of overwrite
_jQuery = window.jQuery,
// Map over the $ in case of overwrite
_$ = window.$;
jQuery.noConflict = function( deep ) {
if ( window.$ === jQuery ) {
window.$ = _$;
}
if ( deep && window.jQuery === jQuery ) {
window.jQuery = _jQuery;
}
return jQuery;
};
// Expose jQuery and $ identifiers, even in AMD
// (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
// and CommonJS for browser emulators (#13566)
if ( typeof noGlobal === strundefined ) {
window.jQuery = window.$ = jQuery;
}
For learning jQuery refer to :-
https://api.jquery.com/
http://learn.jquery.com/
Let's say I have a lot of instances of $(this).show("slow", function(){fVar;}); where $(this) are different objects at different times, but they all need .show("slow", function(){fVar;}); and fVar is a previously defined function var.
Is it possible to set .show("slow", function(){fVar;}); as a var like "myVar" so I could theoretically do something like $(this).myVar();
You sure can. Check out the jquery docs
(function( $ ) {
$.fn.myPlugin = function() {
// Do your awesome plugin stuff here
};
})( jQuery );
don't forget about proper namespacing! And just use the method that you declare (so mPlugin) just as you would any other JQuery method
I'm not so much into JQuery but try this -
$.prototype.myVar=function(){
//here goes your code
}
just create it as a function expression and you can use it wherever you want. this function takes in an event object that is created by an event listener. ie: click functions. use this var as your event function callback and pass in the event object so it knows what to show.
var showSlow = function(eventObject){
$(eventObject).show("slow", function(){
fVar;
});
}
below, it says $ sign refers to jQuery. i need help understand how?
jQuery(function($){
// Here `$` refers to jQuery
});
When you pass a function to jQuery like this:
jQuery(function() {
...
});
It's the same as using jQuery's "document ready" handler:
jQuery(document).ready(function() {
...
});
however the argument passed to that function is actually the global jQuery object itself - it's just that most such handlers never use that parameter. Hence the real signature is this:
jQuery(document).ready(function($) {
...
});
so, within that function $ is a local alias to the global jQuery object. The variable name could be anything you wanted, but $ is a popular alias for jQuery, and the default global alias unless you call jQuery.noConflict().
This is described in more detail in the paragraph "Aliasing the jQuery Namespace" at http://api.jquery.com/ready/
That function is a so called allonymous function where jQuery runs that allonymous function with the first parameter this.
So just for understanding it makes something like this:
function jQuery(xx) {
xx(this);
}
Just an example for show you how is it possibile
function externalLibrary(b) {
if(typeof b === 'function') {
b(externalLibrary);
}else {
// other stuff
}
}
var myFunc = function(aliasOfExternalLibrary) {
// here aliasOfExternalLibrary is a reference to externalLibrary
}
externalLibrary( myFunc );
jQuery do the same (in one more complex system)