Skip to content
Advertisement

why jquery/javascript code get conflict with other jquery/javascript?

While using jquery or javascript code most of the time i have to face the problem of jquery or javascript conflict.

Still i haven’t got the reason why this stuff get conflict with other code?

Any one have any idea about this?

And any solution so that next time this issue will not occur while project development.

I have one solution to stop the conflict of jquery files that is,

    <script>
    var j$=jQuery.noConflict();
    </script>

but all the time this code is not working.

Advertisement

Answer

If you are using another JavaScript library that uses the $ variable, you can run into conflicts with jQuery. In order to avoid these conflicts, you need to put jQuery in no-conflict mode immediately after it is loaded onto the page and before you attempt to use jQuery in your page.

Method 1:

When you put jQuery into no-conflict mode, you have the option of assigning a variable name to replace $. ( only once )

<script src="prototype.js"></script>
<script src="jquery.js"></script>
<script>var $j = jQuery.noConflict();</script> 

Method 2

You can continue to use the standard $ by wrapping your code in a self-executing anonymous function; this is a standard pattern for plugin authoring, where the author cannot know whether another library will have taken over the $.

<script src="jquery.js"></script>
<script>
jQuery.noConflict();

(function($) {
   // your code here, using the $
})(jQuery);
</script>

Method 3:

use jQuery instead of $

there few other ways to do so reference

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement