views:

91

answers:

5
+3  Q: 

jQuery: noConflict

I just cannot work this out. My god it's making my brain hurt, so I turn to you, the good people of the internet.

I've been staring at this, http://api.jquery.com/jQuery.noConflict/ with no luck.

My issue is that the site I'm working on already includes jQuery 1.1.3.1 but I want to do some awesome and snazzy UI work in a few places, but I want to use 1.4.2 for obvious reasons.

I've been trying to run these side by side but I can't seem to get any code to execute. I'm also needing to implement a few plugins using 1.4.2 so I'm not sure if putting jQuery over to something like $j would work, as obviously the plugins would pickup $ and jQuery as 1.1.3.1 rather than 1.4.2

Is there a way that I can wrap my code in a noConflict() block and also include the plugins that I need to create a self contained block of 1.4.2 code?

Any help would be fantastic as my poor mind weeps amid the vast wasteland that is the api docs!

+1  A: 

You should simply upgrade the entire site to 1.4.2.
jQuery does not in general have many breaking changes, so that shouldn't be so hard.

SLaks
I would dearly love to do this, but I would mean massive changes across the whole site, and replacing lots of ajax and plugins also. The whole application folder is 1.22gb so you can imagine the codebase I'm dealing with!
DavidYell
I ended up upgrading the library and rebuilding a bit of the js!
DavidYell
@DavidYell definitely the best solution!
Zeus
+1  A: 

A solid advice: don't stress your and your visitors bandwidth with two jQuery versions. Change your whole codebase to the latest version. It's got less bugs, more support and improved behavior.

galambalazs
Bandwidth isn't a major concern, since it'll be cached and all, I'd be more worried about 2 versions, ya know, breaking everything.
Nick Craver
Oh we have 1.2.1 on there also! ;) ..and my codebase is 1.22gb!
DavidYell
Bandwidth isn't a major concern?? IT IS! You can't count on caching. http://stackoverflow.com/questions/3111468/speed-performance-reduce-http-requests-or-not/3137112#3137112
galambalazs
@galambalazs - Caching is a lot more dependable than your answer suggests, don't dismiss it. But all of that's beside the point. **This will break his page**, the amount of bandwidth used is secondary to none of it working.
Nick Craver
@Nick secondary for you, not so secondary for your users. It's all about users. It's always been... Don't let them suffer for your design decisions.
galambalazs
@galambalazs - I think you completely missed the point here. A broken page is the *primary* concern. You would rather deliver a low-bandwidth completely broken page than a larger yet working one? I don't know of a single developer who would make that decision... **Of course bandwidth is a concern**, but it's *secondary* to making it work in the first place.
Nick Craver
@Nick of course he should make it work, but since the whole development is stuck, why not **leave legacy code behind** and make it work with **less bandwith** and a **more stable library**.
galambalazs
@Nick you see, the point here is he got a chance to start over, if he won't his users will suffer. And they will suffer for a long time, because the application will be bound to the legacy library for another couple of years.
galambalazs
@galambalazs - I don't disagree, I've answered this [plenty of times before](http://stackoverflow.com/questions/3061618/using-different-versions-of-jquery-on-the-same-page/3061647#3061647), nowhere did I advocate leaving 2 libraries, read through my comments or any of my ~2500 answers on this site...I'll never advocate that :) I'm just saying bandwidth is not the **current** problem. Even in your comments you agree the main issue is the **old library**, not the *bandwidth*, it's "secondary". :) Also look at Drew's answer, removing the old library isn't always *your* decision to make.
Nick Craver
A: 

You should post an example that isn't working. I bet we'd clear it up immediately.

I don't believe noConflict is a 'block' exactly. You use noConflict to tell jQuery that you want it to remove anything it declares from the global JavaScript namespace (because you want to load jQuery again and reuse those names).

On a page with more than 1 instance of jQuery loaded, both instances should use noConflict. I don't know if it's possible to load a second instance of jQuery if there's already an instance loaded and that instance didn't call noConflict.

@SLaks and @galambalazs: Sometimes you're writing a small amount of content that will be displayed within a larger page, such as a portal. The portal already uses a(n outdated) version of jQuery, but you need a newer version. Also you don't control the larger portal page, just the content you're writing that will be displayed within it.

This scenario accurately describes real work I do commonly.

Drew Wills
This will cause all sorts of issues, since `jQuery` is the object name, plugins are extending...what? The last one that was loaded before it executes, and any loading jQuery clears all plugins of the one before...this is only the tip of the iceberg in terms of issues, it's just not designed to do this.
Nick Craver
Drew Wills
A: 

I would agree with other posts that suggest trying to upgrade to 1.4.2 across the board... That said, you clearly have a good reason for attempting what you are trying to do. To my knowledge there is no easy way to deal with your situation (as I too have tried something similar).

"noConflict" simply releases the global "$" variable so that other libraries/scripts may safely use it. Given that you are trying to use two versions of jQuery "noConflict" isn't really helpful here...calling it for both versions of jQuery doesn't change the fact that both versions still need to reference the global "jQuery" object (of which there can only be one...).

The issue (as you clearly already know) is that loading the 2nd jQuery version will "clobber" the original jQuery object (and all the "customizations" made to it by plug-ins).

The only reliable (albeit incredibly inefficient) solution I could come up with is:

  • Load the existing (old) jQuery version and plugins (assuming you can't avoid this)
  • Load the new jQuery version
  • RE-load and existing plugins and any new plugin you want to use
  • Test extensively
droo
This had not occured to me, but it's a great suggestion. Unless I can persuade my boss to let me upgrade everything! ;)
DavidYell
A: 

I had a similar issue recently where I was using jQuery v1.3.2 on a page but a 3rd party questionnaire popup was using an older version on the same page. I eventually managed to solve the problem. I referenced jQuery 1.3.2 as follows:

 <script type="text/javascript" src"/Scripts/jquery-1.3.2.min.js"></script>
    <script type="text/javascript"> 
        jq132 = jQuery.noConflict(true); 
 </script> 

I then modified all my jQuery code to use jq132 instead of $. I then did a find and replace on all of the plugins I was using to replace "$(" with "jq132(" and "$." with "jq132.". There may be a more elegant approach but this worked for me. I'm far from a jQuery expert so there may be other $ syntax that you need to handle (i.e. not just "." and "(").) .

j.strugnell