admin管理员组

文章数量:1401171

So I'm developing on a platform that injects jQuery-1.2.6 every time when a page loads. I developed my page to use jQuery-1.4.2, so when my page loads there are 2 jQuery instances on my page.

To avoid any conflict, I've included the following line in my JS file that is outside my page:

var mc$ = jQuery.noConflict();
jQuery(function($)

Everything seems to be working if I use mc$ for where the $ was originally used.

I am getting an error in Firebug and IE that I don't know how to resolve.

Within a file that is injected on the platform when my page loads the developer has done:

 $(document).ready(function() {

and the error in Firebug states: '$ is not a function'.

This error only started to show up once I made the change of:

var mc$ = jQuery.noConflict();

I'm thinking that by creating the mc$ variable that something broke with the developer's $(document)... call. If so, I don't know how to resolve this and if not I would hope there is a solution that I can put into place to provide the functionality back to the developer's injected file.

Thanks!

So I'm developing on a platform that injects jQuery-1.2.6 every time when a page loads. I developed my page to use jQuery-1.4.2, so when my page loads there are 2 jQuery instances on my page.

To avoid any conflict, I've included the following line in my JS file that is outside my page:

var mc$ = jQuery.noConflict();
jQuery(function($)

Everything seems to be working if I use mc$ for where the $ was originally used.

I am getting an error in Firebug and IE that I don't know how to resolve.

Within a file that is injected on the platform when my page loads the developer has done:

 $(document).ready(function() {

and the error in Firebug states: '$ is not a function'.

This error only started to show up once I made the change of:

var mc$ = jQuery.noConflict();

I'm thinking that by creating the mc$ variable that something broke with the developer's $(document)... call. If so, I don't know how to resolve this and if not I would hope there is a solution that I can put into place to provide the functionality back to the developer's injected file.

Thanks!

Share Improve this question edited Jun 4, 2011 at 23:46 Mark asked Jun 4, 2011 at 22:59 MarkMark 3076 silver badges15 bronze badges 4
  • .noConflict() is for patibility with other JavaScript libraries/frameworks, multiple instances of jQuery (sans .sub() in 1.6+) in the same page is not supported in any way. – Nick Craver Commented Jun 4, 2011 at 23:55
  • 3 @Nick Craver the noConflict() documentation disagrees with you. It specifically mentions passing true as an argument to deconflict the jQuery symbol as well. – lawnsea Commented Jun 5, 2011 at 0:17
  • @lawnsea - that's specifically (and limited at that) to 1.6+ (my mentioned exception above). Note that you'll still have many problems even then, for example plugins are defined on the jQuery object, as are (likely) many event handlers, etc depending on jQuery or their plugins. This es from experience addressing these...remember if there weren't plugins, etc...there would be no reason to keep the old version in the first place. – Nick Craver Commented Jun 5, 2011 at 4:39
  • @Nick - Please accept my apology. I did the due diligence I should have done earlier and took a look back in jQuery doc history and found that you're pletely right. I'll donate the spurious upvotes to a good cause. – lawnsea Commented Jun 5, 2011 at 6:38
Add a ment  | 

3 Answers 3

Reset to default 4

The root cause is loading two instances of jQuery into one page. By the sounds of it, the original instance (1.2.6) of jQuery is also being unbound from the global $ variable.

From looking at http://api.jquery./jQuery.noConflict/, you could try supplying an additional parameter to the jQuery.noConflict() call to denote unloading jQuery as well, as it seems your instance of 1.4.2 is being loaded before 1.2.6.

Beyond that, without testing that exact scenario I don't know what other help to offer :)

A call order in your <head> section must be next:

  1. jQuery library №1;
  2. noConflict for it;
  3. jQuery library №2.

.noConflict() can look like this:

<script type="text/javascript">var mc$ = jQuery.noConflict();</script>

And after use this syntax in the scripts:

mc$('#container').height();

You may find this answer helpful: jquery noConflict not working in IE8 only

本文标签: javascriptjQuery noConflict() problemStack Overflow