views:

44

answers:

2

i have seen a jquery code that contains jQuery.noConflict method.

do not know purpose of it, didnt get the why we need that.

i only understand that it is something related to jquery plugin.

please help me to understand.

+3  A: 

It is especially useful when there are more than one javascript libraries used on a page like jQuery, prototype, etc. The $ character is special in those libraris as in jQuery. For this reason, jQuery.noConflict.

See:

Using jQuery with Other Libraries (Official Docs)

Update:

Once you have used jQuery.noConflict, the control of $ is handed over to other libraries rather than jQuery and in this case, you will have to use jQuery rather than $.

Other alternative that turns out to be especially useful when writing jQuery plugins is something like:

(function($){
 // your code....
})(jQuery);

In this case though, you can use the $ normally even if other libraries are included on the page.

Sarfraz
ok, can you explain me in a simple way how it works, and its usage
cache
@cache: See the link in my answer.
Sarfraz
confused. but how they identify which $ is this
cache
+1  A: 

jQuery (and other frameworks like it) store an alias of the jQuery object in the $ variable. It's just for easy shorthand when you write code.

You could actually write all your code like this jQuery('.selector'); instead of using the dollar sign if you wanted to. The no-conflict mode allows you to use other code (usually another framework) that ALSO stores something in the dollar sign variable.

Stephen
confused. but how they identify which $ is this
cache
When you use noConflict, you release the jQuery alias from the dollar sign. So, for the rest of your code, you can only use this syntax: `jQuery('.whatever');` for jQuery actions, and you can use the dollar sign for the other framework.
Stephen
thank you very much
cache
you're welcome!
Stephen