-
Notifications
You must be signed in to change notification settings - Fork 20.6k
Ability to avoid expando collisions #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to avoid expando collisions #29
Conversation
…ix. Also put the version number in the expando. Doesn't allow prefix after jQuery.cache has been used. Fixes #6842.
What about just moving expando defn up to core.js, and altering like so:
That way it's only a single line change and should to never conflict. |
That ignores the possibility of the other jQuery being noConflicted as well... unlikely but more common then you'd probably guess. |
I am wondering, why not to set the expando to the window to "reserve" it..
|
I was avoiding leaking anything else into the global namespace. |
Ok, but there is still possible that same prefix is used.. an random number would be even more solid for me. |
Also it is possible to loop while a millisecond is passed...
|
The developer could at least ensure that it's unique. The wait a ms method is ok, but it seems a little greedy to double the time via a blocking loop that jQuery takes to instantiate. |
IMO looping one ms is not so greedy as expecting from the user to provide unique prefixes only because internally needed. Also take in consideration that the expando could be used in plugins before the noConflict call. For example because the data() and event API can be used against native objects too. |
That's penalizing all users for the need of 1% of users. If you know you need to ensure expando uniqueness, you should be able to prefix it. |
I test against the case that the expando has already been used. |
Are you saying that only because the cache is used that one ms is certainty passed, even if entire jQuery initialization takes less then one ms? If not, you should at least raise an error if a prefix is given and cache is not empty. |
Also we can loop one ms in outro.js so there would not be penalties (entire jQuery init takes normally ~5 ms).
|
Sorry, I realized that I was not clear enough...
As I said, data() and event API can be used against native objects too. And in case of natives, expando is used without using the cache. But even if your test was good enough, silently not applied prefixes (in case of empty cache) is dangerous if jQuery initialization takes less then 1ms. |
What if we just changed expando: ... to: expando: "jQuery" + (Math.random() * 10E15), That will be damn hard to duplicate (probably akin to winning a few lotteries simultaneously). |
IMO, that would be enough if it is hard to expect hundreds of jQuery instances in same window. Is it? |
It's a bit of a hack, but I don't think I mind... I still think we should add the $.fn.jquery string in there since a lot of times the reason for loading to jQuery's is for two different versions. |
As long as
This keeps the |
@cowboy: EDIT: THe first reason is not entirely true, becouse you could set |
Regarding the random number solution, although in my opinion it is a fine solution, I am a bit worried about the possibility that the random generator is not always good enough as we could expect (precision, poor implementation, eventual OS bug..). |
|
@paulirish: Heh, you could be surprised what some people do in name of optimization (mobile)... but probably you are right, probably I am too worried about that. :) |
@rkatik my point is simply that in Alex's use-case it makes more sense to manipulate the Also, I'd use |
The potential bug I reported is here: http://dev.jquery.com/ticket/6842
I want to be able to guarantee that my expando doesn't collide with another. V8 is now scarily close to being able to instantiate two instances of jQuery in the same millisecond. The only way I can think of to reliably do this (keeping in mind the case where two copies of jQuery are deeply noConflicted), is to explicitly pass in a unique prefix.
This patch reduces normal conflicts between different versions of jQuery by adding in the version to the expando, and also exposes a second parameter to jQuery.noConflict
For example:
myJQ = jQuery.noConflict(true, 'myUniquePrefix');Generally, this is necessary for third party widgets that use jQuery and don't want to collide with whatever is on the page.
I did add a check to not allow a prefix to be added if the jQuery.cache had been used, that way no connections would be lost between old expando values. The best-practice here being that you call this new noConflict(true, 'prefix') before you use anything in the library.
Test cases are provided (and passing) for both the case when a prefix is added with or without an empty jQuery.cache.