2009/4/22 John Resig <[email protected]>:
>
> Hmm... I do understand your predicament (wanting the
> dynamically-loaded script to use the right version of jQuery). I'm
> trying to imagine situations where your change could cause potential
> problems, but I'm not sure, yet.
Yeah, that's why I thought I'd post this - I currently have a separate
function for inject html into the dom that swaps the globals and calls
.html() - I thought it would be better done in the globalEval() fn
instead.
> FWIW, this problem also affects getScript when pulling scripts from an
> outside domain (and it can't be fixed used the technique described
> here since there's no window of opportunity where the global jQuery
> variable could be changed).
That doesn't really affect us, not pulling from another domain, but I
can see the problem here.
> I also note that you only change 'jQuery' and not '$' - it seems like
> both would be necessary, in this case.
In our environment $ is not jQuery, in fact, sometimes it may be Prototype.
So I guess it would only be replaced conditionally.
> But, again, I'm worried about the external affects of manipulating the
> global variables.
>
> --John
Some people have mentioned sandboxing using iframes, but this really
isn't an option in our situation.
Well, I've tried all manner of approaches to deal with scripts from
other domains.
The patch below is best compromise I can find, it has a few issues though:
* Doesn't restore original global if a script fails to load.
* An event may occur whilst the scripts are loading async - and the
globals will be wrong - although if all the code is written well, ie
using (function($) { ... })(jQuery) then this shouldn't be a problem.
Is there any way of forcing the script loading to be synchronous?
Demo can be found here:
http://test3.internal.adaptavist.net/~mgibson/versions/load2.html
Tested and works in:
Linux: Firefox 3.0.8, Opera 9.64, Konqueror 4.2.0
WinXP: Safari 3.1.2, IE6, IE7, Chrome 1.0.154.53
Patch:
Index: src/ajax.js
===================================================================
--- src/ajax.js (revision 6320)
+++ src/ajax.js (working copy)
@@ -265,6 +265,7 @@
if ( !done && (!this.readyState ||
this.readyState ==
"loaded" || this.readyState == "complete") ) {
done = true;
+ jQuery.globalSwap(false);
success();
complete();
@@ -273,6 +274,8 @@
head.removeChild( script );
}
};
+
+ jQuery.globalSwap(true);
}
// Use insertBefore instead of appendChild to
circumvent an IE6 bug.
Index: src/core.js
===================================================================
--- src/core.js (revision 6320)
+++ src/core.js (working copy)
@@ -256,6 +256,9 @@
return target;
};
+var globalSwapCount = 0,
+ globalStore;
+
jQuery.extend({
noConflict: function( deep ) {
window.$ = _$;
@@ -283,6 +286,30 @@
return elem.nodeType === 9 && elem.documentElement.nodeName !==
"HTML" ||
!!elem.ownerDocument &&
elem.ownerDocument.documentElement.nodeName !== "HTML";
},
+
+ globalSwap: function( save ) {
+ if ( save ) {
+ // Save global jQuery/$
+ if ( !globalSwapCount++ ) {
+ $.each(['jQuery', '$'], function() {
+ var g = window[this];
+ if ( g && g.fn && g.fn.jquery && g !==
jQuery ) {
+ globalStore = globalStore || {};
+ globalStore[this] = g;
+ window[this] = jQuery;
+ }
+ });
+ }
+ } else {
+ // Restore global jQuery/$
+ if ( !--globalSwapCount && globalStore ) {
+ $.each(globalStore, function(k, v) {
+ window[k] = v;
+ });
+ globalStore = undefined;
+ }
+ }
+ },
// Evalulates a script in a global context
globalEval: function( data ) {
@@ -299,10 +326,14 @@
script.text = data;
}
+ jQuery.globalSwap(true);
+
// Use insertBefore instead of appendChild to
circumvent an IE6 bug.
// This arises when a base node is used (#2709).
head.insertBefore( script, head.firstChild );
head.removeChild( script );
+
+ jQuery.globalSwap(false);
}
},
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---