Bug Tracker

Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#5880 closed feature (duplicate)

Method to create a copy of jQuery

Reported by: shadedecho Owned by:
Priority: low Milestone: 1.5
Component: core Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:


For those of us who work with including jQuery (along with a mashup/widget) into abritrary pages that we don't necessarily control, having "jQuery.noConflict()" is absolutely awesome. However, there's a use case that I've run into a few times now, which is, sometimes you might need to create a sandbox'd copy/clone of jQuery (for the purposes of adding in plugins for instance), that remains separate from the existing copy of jQuery that might be on the page.

Apparently some have explored ways to create a clone of the jQuery object, but what it comes down to is that there are some private pieces of data which are held up in the closure that the jQuery function creates, which cannot be separated as it is currently. So, it's not possible to create a totally (only mostly) separate copy of jQuery from an existing instance.

The trick I'm currently using, which is a little brittle and ugly, is to search the DOM looking for the script tag that loaded jQuery (only if it matches a regex with "jquery.js" or some variation thereof in it)... if the script is found, then a new script element is created with the same source URL, which should re-request jQuery (from the cache hopefully!) and then re-execute jQuery.

Then, a jQuery.noConflict(true) call will roll back to the original copy of jQuery and the new sandboxed copy can be saved and used.

The worst part about this hack is the brittleness of having to assume that jQuery comes in its own script file, and that the name is something predictable. Secondarily, having to modify the DOM (even just a script tag) is less than desirable. But it seems to work in most cases.

So, my request is, that we get a new core function, like $.clone (not $.fn.clone), that will in some way re-execute jQuery and create a new closure and new object instance, returning that sandbox'd copy for later use.

Change History (7)

comment:1 Changed 12 years ago by dmethvin

Keywords: needsreview added
Milestone: 1.4.1
Status: newopen
Summary: a $/jQuery core "clone" functionMethod to create a copy of jQuery

I am not understanding the use case for this from the description, so I'm marking it for review.

comment:2 Changed 12 years ago by shadedecho

So, basically, if you want to have multiple copies of jQuery on the same page (and by copies, I don't just mean references), because for instance you may need to use plugins that conflict, or you may just want to sandbox away some copy of jQuery and prevent changes to that copy from affecting jQuery on the rest of the page.

We have noConflict() which can roll back the global jQuery references, but to create multiple jQuery copies with noConflict() you actually have to load jQuery a second or third time. It would be more preferable if you could just take an existing jQuery instance, and call $.clone() on it to get a new re-executed closure. That way, you'd have two entirely separate copies of jQuery without needing to load the script a second time.

As I said before, this is primarily useful in the mashup case where you load a widget onto a page that loads its own copy of jQuery, and you want to be able to keep that copy separate and sandboxed from the existing instnace. That way, if the page your widget is loaded onto already has jQuery, and several plugins loaded onto it, you can get a fresh clean copy of jQuery that your widget can modify and not affect the rest of the page.

My thought on how to accomplish this is to have the entire jQuery inside a named self-executing function (which establishes the main closure for the jQuery instance), and then the clone() function could call that named closure function a second time to generate a new copy of jQuery and return the result.

comment:3 Changed 12 years ago by gnarf

Is it just me or does http://api.jquery.com/jQuery.sub/ complete this?

comment:4 Changed 12 years ago by jitter

Keywords: needsreview removed
Milestone: 1.5
Priority: minorlow
Resolution: duplicate
Status: openclosed

A first step in this direction has been taken with jQuery.sub although it is different from a completely new loaded version of jQuery. Some further enhancements to jQuery.sub are planned for 1.6

comment:5 Changed 12 years ago by jitter

Duplicate of #7979.

comment:6 Changed 12 years ago by shadedecho

If I load a widget onto a page that already has jquery, and my widget wants to use a pristine/fresh copy of jquery, and so it tries to subclass the existing jquery... doesn't that mean that it'll just create a copy of the jquery as it exists then (which may already be extended and not pristine)?

The spirit of my request was to be able to get a pristine fresh copy of jQuery from any jQuery, regardless of its current state.

Perhaps I'm misunderstanding the future plans for sub() but I think it's not giving what this ticket is asking for.

comment:7 in reply to:  6 Changed 12 years ago by scottgonzalez

Replying to shadedecho:

Perhaps I'm misunderstanding the future plans for sub() but I think it's not giving what this ticket is asking for.

Yes, this seems to be outside of the scope for jQuery.subClass() (at least based on current plans). Perhaps this could be implemented via jQuery.subClass( true ) or something similar, but this ticket should remain open as it is not a duplicate of #7979.

Note: See TracTickets for help on using tickets.