Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement JQuery.noConflict() ?

I am using both javascript and jquery code on the same html page. For some reason, the jQuery library is stopping my native javascript code from working properly.

I found this page: jQuery No Conflict that says you can use a jquery.noConflict to release $ back to javascript. However, I'm not sure how to do this?

Specifically, I'm not sure how to implement this correctly? Where does the the Jquery code go, where does the JS code go?

My code is below:

<script type="text/javascript">   $.noConflict();   // Code that uses other library's $ can follow here. </script> 
like image 624
SuperLuigi Avatar asked Oct 24 '11 21:10

SuperLuigi


People also ask

What is the use of noConflict () method in jQuery?

jQuery Misc noConflict() Method The noConflict() method releases jQuery's control of the $ variable. This method can also be used to specify a new custom name for the jQuery variable. Tip: This method is useful when other JavaScript libraries use the $ for their functions.

How do you resolve JavaScript conflicts?

Solution 1Go for the master page on those scripts that are required in all the pages that inherits the master page. Add only those script code which is required in your page level specifically. Avoid adding the scripts again and again. That will create conflict in choosing the script.

Can you use any other name in place of in jQuery?

In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $ . If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.

How do I put multiple Jquerys into one page?

Yes, you can use multiple versions of jQuery on the same page. To avoid any kind of conflict, use the jQuery. noConflict() method.


1 Answers

jQuery.noConflict will reset the $ variable so it's no longer an alias of jQuery. Aside from just calling it once, there's not much else you really need to do. Though, you can create your own alias with the return value, if you'd like:

var jq = jQuery.noConflict(); 

And, generally, you want to do this right after including jQuery and any plugins:

<script type="text/javascript" src="/path/to/jquery.js"></script> <script type="text/javascript" src="/path/to/jquery-plugin.js"></script> <script type="text/javascript">   jQuery.noConflict();   // Code that uses other library's $ can follow here. </script> <script type="text/javascript" src="/path/to/prototype.js"></script> 

You can also go one step further and free up jQuery with noConflict(true). Though, if you take this route, you'll definitely want an alias as neither $ nor jQuery will probably be what you want:

var jq = jQuery.noConflict(true); 

I think this last option is mostly used for mixing versions of jQuery, particularly for out-dated plugins when you want to update jQuery itself:

<script type="text/javascript" src="jquery-1.4.4.js"></script> <script type="text/javascript" src="jquery-older-plugin.js"></script> <script type="text/javascript">     var jq144 = jQuery.noConflict(true); </script> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript" src="jquery-newer-plugin.js"></script> 
like image 184
Jonathan Lonowski Avatar answered Oct 01 '22 23:10

Jonathan Lonowski