JQuery No-Conflict And Using Different Versions Of jQuery

In this post we will see what is jQuery no-conflict and what is its importance. We will also learn how can we use different versions of jQuery in the same page according to your need. jQuery no-conflict is an option given by jQuery to overcome the conflicts between the different js frameworks or libraries. When we use jQuery no-conflict mode, we are replacing the $ to a new variable and assigning to jQuery. Here in this post we will explain this feature in depth and we will also create a demo of using different versions of jQuery together in one page. I hope you will like this.

Please see this article in my blog here.

Download Source Code

Please download the source code here.

Introduction

As you all know, some other JavaScript libraries also use the $ (Which is the default reference of jQuery) as a function or variable name as jQuery has. And in our development life, we are not at all strict to only jQuery, we may use lots of other libraries too. Isn’t it? I use different libraries in my projects to accomplish different tasks. So what will happen when we use those libraries? There will be conflicts between those libraries right since they all use $ as the variable name. The situation is really bad.

So we have only one chocolate and more number of winners, in real life we can just cut the chocolate into the number of winners, right?

Dogs Sharing Food

But that won’t be accepted in this case, so we have an another option, this option is introduced by jQuery and it is jQuery no-conflict.

Background

I always use different libraries in my project, thus sometimes I faced this problem in my project. So I thought of sharing with you all who are facing it.

Using the code

The first thing you want to do is creating an HTML page.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>JQuery noConflict - Sibeesh Passion</title>  
  6. </head>  
  7.   
  8. <body>  
  9.     JQuery noConflict - Sibeesh Passion  
  10. </body>  
  11.   
  12. </html>   

So what is next? Adding references? Yes.

  1. <script src="prototype.js"></script>  
  2. <script src="JQuery%20Versions/jquery-1.11.1.min.js"></script>   

So we are using prototype lowest version and jQuery together, in this case you will face conflict errors, and please be noted that the new version of prototype.js doesn’t have any conflict with jQuery since they updated and given the fix.

So what to do if you found any issues? You just need to give the $ to prototype and assign a new variable to jQuery as follows.

  1. <script>  
  2.    // Give $ back to prototype.js; create new alias to jQuery.  
  3.    var $jq = jQuery.noConflict();  
  4. </script>   

And now you can use $jq as the new jQuery reference.

  1. <script>  
  2.     $jq(document).ready(function() {  
  3.         $jq("#btn").on('click', function() {  
  4.             alert('Clicked');  
  5.         });  
  6.     });  
  7. </script>  

Complete Code

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>JQuery noConflict - Sibeesh Passion</title>  
  6.     <script src="prototype.js"></script>  
  7.     <script src="JQuery%20Versions/jquery-1.11.1.min.js"></script>  
  8.     <script>  
  9.         // Give $ to prototype.js  
  10.         var $jq = jQuery.noConflict();  
  11.     </script>  
  12.     <script>  
  13.         $jq(document).ready(function() {  
  14.             $jq("#btn").on('click', function() {  
  15.                 alert('Clicked');  
  16.             });  
  17.         });  
  18.     </script>  
  19. </head>  
  20.   
  21. <body>  
  22.     JQuery noConflict - Sibeesh Passion  
  23.     <br />  
  24.     <br />  
  25.     <br />  
  26.     <input type="button" value="" id='btn' />  
  27. </body>  
  28.   
  29. </html>   

Sometimes we may end up in a situation to use different versions of jQuery in the same page, right?

Situation I faced

I was working in an old project which was handled by a team few years back, at that time I got a minor work to do in that project, to do the task I was in a need to add the latest version of jQuery since the entire project was using the old reference of jQuery. I was in a situation that I should not remove the old references and update with the new one, if I do that their might be some issues with other functionalities, right? Since the project was already live, I didn’t took that risk. So I added the new version and used jQuery noConflict method to avoid the reference issues.

Using the code

Here I am going to give you a demo of how to use different versions of jQuery in one page.

First of all please add the needed reference to your page.

  1. <script src="JQuery%20Versions/jquery-1.9.0.min.js"></script>  
  2. <script src="JQuery%20Versions/jquery-1.10.1.min.js"></script>  
  3. <script src="JQuery%20Versions/jquery-1.11.0.min.js"></script>  
  4. <script src="JQuery%20Versions/jquery-1.11.1.min.js"></script>  
  5. <script src="JQuery%20Versions/jquery-1.11.3.min.js"></script>   

Please be noted that we have added jQuery 1.9.0,1.10.1,1.11.0,1.11.1,1.11.3 in our page.

Now we need to add some buttons in the UI and later we will bind the click events of the same buttons.

  1. <input type="button" value="Use jQuery 1.9.0" id='btn190' />  
  2. <input type="button" value="Use jQuery 1.10.1" id='btn1101' />  
  3. <input type="button" value="Use jQuery 1.11.0" id='btn1110' />  
  4. <input type="button" value="Use jQuery 1.11.1" id='btn1111' />  
  5. <input type="button" value="Use jQuery 1.11.3" id='btn1113' />   

What is next, so we added the different jQuery versions. Now we will create different variable names for each versions. Is that fine?

  1. <script src="JQuery%20Versions/jquery-1.9.0.min.js"></script>  
  2. <script>  
  3.     var jQuery190 = jQuery.noConflict();  
  4.     window.jQuery = jQuery190;  
  5. </script>  
  6. <script src="JQuery%20Versions/jquery-1.10.1.min.js"></script>  
  7. <script>  
  8.     var jQuery1101 = jQuery.noConflict();  
  9.     window.jQuery = jQuery1101;  
  10. </script>  
  11. <script src="JQuery%20Versions/jquery-1.11.0.min.js"></script>  
  12. <script>  
  13.     var jQuery1110 = jQuery.noConflict();  
  14.     window.jQuery = jQuery1110;  
  15. </script>  
  16. <script src="JQuery%20Versions/jquery-1.11.1.min.js"></script>  
  17. <script>  
  18.     var jQuery1111 = jQuery.noConflict();  
  19.     window.jQuery = jQuery1111;  
  20. </script>  
  21. <script src="JQuery%20Versions/jquery-1.11.3.min.js"></script>  
  22. <script>  
  23.     var jQuery1113 = jQuery.noConflict();  
  24.     window.jQuery = jQuery1113;  
  25. </script>   

So now we have added variable names for all the versions. The next thing we are going to do is to fire the click events for each versions.

  1. <script>  
  2.     jQuery190(document).ready(function($) {  
  3.         //The codes for jQuery 1-9-0  
  4.         $("#btn190").on('click', function() {  
  5.             alert($.fn.jquery);  
  6.         })  
  7.     });  
  8.     jQuery1101(document).ready(function($) {  
  9.         //The codes for jQuery 1-10-1  
  10.         $("#btn1101").on('click', function() {  
  11.             alert($.fn.jquery);  
  12.         })  
  13.     });  
  14.     jQuery1110(document).ready(function($) {  
  15.         //The codes for jQuery 1-11-0  
  16.         $("#btn1110").on('click', function() {  
  17.             alert($.fn.jquery);  
  18.         })  
  19.     });  
  20.     jQuery1111(document).ready(function($) {  
  21.         //The codes for jQuery 1-11-1  
  22.         $("#btn1111").on('click', function() {  
  23.             alert($.fn.jquery);  
  24.         })  
  25.     });  
  26.     jQuery1113(document).ready(function($) {  
  27.         //The codes for jQuery 1-11-3  
  28.         $("#btn1113").on('click', function() {  
  29.             alert($.fn.jquery);  
  30.         })  
  31.     });  
  32. </script>   

So if you run your page and click the buttons, you can find that the related codes only get fired.

use jQuery

page on local host

Now what if you comment out the variable declaration for jQuery version 1.11.3?

  1. //var jQuery1113 = jQuery.noConflict();  
  2. //window.jQuery = jQuery1113;   

You will get an error: "Uncaught ReferenceError: jQuery1113 is not defined" in browser console.

jQuery no-conflict And Using Different Versions Of JQuery
Complete Code
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Use JQuery Different Versions in One Page - Sibeesh Passion</title>  
  6.     <script src="JQuery%20Versions/jquery-1.9.0.min.js"></script>  
  7.     <script>  
  8.         var jQuery190 = jQuery.noConflict();  
  9.         window.jQuery = jQuery190;  
  10.     </script>  
  11.     <script src="JQuery%20Versions/jquery-1.10.1.min.js"></script>  
  12.     <script>  
  13.         var jQuery1101 = jQuery.noConflict();  
  14.         window.jQuery = jQuery1101;  
  15.     </script>  
  16.     <script src="JQuery%20Versions/jquery-1.11.0.min.js"></script>  
  17.     <script>  
  18.         var jQuery1110 = jQuery.noConflict();  
  19.         window.jQuery = jQuery1110;  
  20.     </script>  
  21.     <script src="JQuery%20Versions/jquery-1.11.1.min.js"></script>  
  22.     <script>  
  23.         var jQuery1111 = jQuery.noConflict();  
  24.         window.jQuery = jQuery1111;  
  25.     </script>  
  26.     <script src="JQuery%20Versions/jquery-1.11.3.min.js"></script>  
  27.     <script>  
  28.         //var jQuery1113 = jQuery.noConflict();  
  29.         //window.jQuery = jQuery1113;  
  30.     </script>  
  31.     <script>  
  32.         jQuery190(document).ready(function($) {  
  33.             //The codes for jQuery 1-9-0  
  34.             $("#btn190").on('click', function() {  
  35.                 alert($.fn.jquery);  
  36.             })  
  37.         });  
  38.         jQuery1101(document).ready(function($) {  
  39.             //The codes for jQuery 1-10-1  
  40.             $("#btn1101").on('click', function() {  
  41.                 alert($.fn.jquery);  
  42.             })  
  43.         });  
  44.         jQuery1110(document).ready(function($) {  
  45.             //The codes for jQuery 1-11-0  
  46.             $("#btn1110").on('click', function() {  
  47.                 alert($.fn.jquery);  
  48.             })  
  49.         });  
  50.         jQuery1111(document).ready(function($) {  
  51.             //The codes for jQuery 1-11-1  
  52.             $("#btn1111").on('click', function() {  
  53.                 alert($.fn.jquery);  
  54.             })  
  55.         });  
  56.         jQuery1113(document).ready(function($) {  
  57.             //The codes for jQuery 1-11-3  
  58.             $("#btn1113").on('click', function() {  
  59.                 alert($.fn.jquery);  
  60.             })  
  61.         });  
  62.     </script>  
  63. </head>  
  64.   
  65. <body>  
  66.     Use JQuery Different Versions in One Page  
  67.     <br />  
  68.     <br />  
  69.     <br />  
  70.     <input type="button" value="Use jQuery 1.9.0" id='btn190' />  
  71.     <input type="button" value="Use jQuery 1.10.1" id='btn1101' />  
  72.     <input type="button" value="Use jQuery 1.11.0" id='btn1110' />  
  73.     <input type="button" value="Use jQuery 1.11.1" id='btn1111' />  
  74.     <input type="button" value="Use jQuery 1.11.3" id='btn1113' />  
  75. </body>  
  76.   
  77. </html>  
That’s all. 

Conclusion

Did I miss anything that you may think which is needed? Have you ever used different versions of jQuery in same page? Have you ever tried jQuery noConflict? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Your turn. What do you think?

If you have any questions, then please mention it in the comments section.

Up Next
    Ebook Download
    View all
    Learn
    View all