Hello,
Some time ago, I also had issues with $ and jQuery. I’ll try to give below some explanation about them.
In fact $ is a shortcut for the "jQuery" object which is defined by the jQuery library.
But $ can be used by many scripts and also non-jQuery libraries like Mootools. From there the risk of conflicts. Even if your site doesn’t use Mootools, you can encounter problems. Personnaly, I didn’t use jQuery.noconflict()
In general this works:
(function (some_optional_argument) {
Your jQuery script.
}) (jQuery)
Well, what does this strange syntax mean exactly?
Inside the outermost "(...)" brackets, your define a function, like:
function TheFunctionName(some_optional_argument) {
What the function makes
}
How would you call a function that has no argument? This way:
Now, in Javascript, you can define anonymous functions, that is functions that don’t have any name. That’s what we do:
function (some_optional_argument) {}
This function has no name, but receives an argument.
The
that ends my first example corresponds to
, but here, we simply evaluate the function that was defined just before. From here the importance of the () brackets around the function definition. Since this function does not have any name, the function call is simply
As you can see, we pass the "jQuery" object as argument to the function. This object was transparently defined when loading the jQuery library.
Here is some typical code I use and that avoids some problems due to the "$"
(function(jQuery) {
...
)(jQuery)
This should help. At least, I hope so.
Julien