2

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.

8
  • 2
    Can you provide an example test case where this doesn't work? Commented Feb 17, 2012 at 7:42
  • in almost all the code this happen.write now i cant provide code. Commented Feb 17, 2012 at 7:44
  • 1
    What kind of conflicts you're talking about? Most common usage for noConflict() is for jQuery to get along with other possible libraries that make use of $ variable (for example Prototype is such library). So if you often work with two conflicting libraries on one page, you have to get used to using jQuery.noConflict. Commented Feb 17, 2012 at 7:44
  • @ArpiPatel : You just need to write one time jQuery.noConflicts(), may b in header. Commented Feb 17, 2012 at 7:47
  • 1
    @diEcho @ArpiPatel - to further elaborate on diEcho comment - use jQuery.noConflict once somewhere after embedding jQuery into to page, and then, instead of dollar sign variable ($) refer to jQuery by using variable you have assigned it to (j$ in your example). Commented Feb 17, 2012 at 7:50

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

1

For jQuery, it's explained in noConflict's API page; basically, many JS frameworks use $ as a function name, and so they may conflict if more than one is used. Which it shouldn't anyway.

Comments

1

Try placing this in your header:

<script type="text/javascript" 
    src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js">   </script>

Try to give an alert when DOM is ready and find it alerts or not if not there is a conflict

<script>
$(document).ready(function () {
alert("Hello!");
});
</script>

I there is conflict try this:

<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  $.noConflict();
  jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
  });
  // Code that uses other library's $ can follow here.
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.