I'm trying to embed a web application using jQueryUI I have into a third-party site. My web application uses jQuery & jQueryUI (mainly for autocomplete and a few smaller things). The third-party site also uses both.
My code assumes a very recent version of jQueryUI. The third-party site could contain a much older version.
What are my options? I have seen conflicting reports as to whether jQueryUI can be loaded twice or can only be loaded once. However, this SO question suggests it is possible if jQueryUI is built with 'CONTEXT', but I can't find any reference to what 'CONTEXT' means.
Here in outline is what I'm trying to do:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<!-- Some version of jQuery and jQueryUI - may change -->
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.18/jquery-ui.min.js"></script>
</head>
<body>
<script type="text/javascript">
// some third-party jQuery & jQueryUI stuff here
</script>
<!-- Up to this point, I cannot make any changes to the code, as all of the above is third-party code -->
<!-- Start of MY application -->
<!-- Load jQuery and jQueryUI -->
<script src="/jquery-ui-1.10.4.custom/js/jquery-1.10.2.js"></script>
<script src="/jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.js"></script>
<!-- Application code, which wants to use jquery-1.10.2 and jquery-ui-1.10.4 -->
<input name="foo" />
<script language="javascript" type="text/javascript">
alert('START OF AUTOCOMPLETE jQuery version: ' + $.fn.jquery + '; jQueryUI version: ' + $.ui.version);
function addAutocomplete (inputElement, options) {
// Run on document ready
$(function() {
alert('WITHIN CODE: ' + $.fn.jquery + '; jQueryUI version: ' + $.ui.version);
<!-- *** Currently this gives "WITHIN CODE: 1.7.1; jQueryUI version: 1.8.18" -->
<!-- *** But what I want is WITHIN CODE: 1.10.2; jQueryUI version: 1.10.4" -->
})
};
addAutocomplete('foo');
</script>
<!-- End of MY application -->
<!-- From this point, I cannot make changes to the code - is third-party code which could include jQuery/jQueryUI code potentially -->
</body>
</html>
What are my options? I'm tearing my hair out over this!
1) I've tried reassigning $
and jquery
, as suggested on this page, but I cannot see why the code at *** shows the old version of jQueryUI:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<!-- Some version of jQuery and jQueryUI - may change -->
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.18/jquery-ui.min.js"></script>
</head>
<body>
<script type="text/javascript">
// some third-party jQuery & jQueryUI stuff here
</script>
<!-- Up to this point, I cannot make any changes to the code, as all of the above is third-party code -->
<!-- Start of MY application -->
<script type="text/javascript">
alert('START jQuery version: ' + $.fn.jquery + '; jQueryUI version: ' + $.ui.version);
var oldJQuery = jQuery;
var oldDollar = $;
</script>
<script src="/jquery-ui-1.10.4.custom/js/jquery-1.10.2.js"></script>
<script src="/jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.js"></script>
<script type="text/javascript">
alert('REPLACED jQuery version: ' + $.fn.jquery + '; jQueryUI version: ' + $.ui.version);
</script>
<!-- Application code, which wants to use jquery-1.10.2 and jquery-ui-1.10.4 -->
<input name="foo" />
<script language="javascript" type="text/javascript">
alert('START OF AUTOCOMPLETE jQuery version: ' + $.fn.jquery + '; jQueryUI version: ' + $.ui.version);
function addAutocomplete (inputElement, options) {
// Run on document ready
$(function() {
alert('WITHIN CODE: ' + $.fn.jquery + '; jQueryUI version: ' + $.ui.version);
<!-- *** Currently this gives "WITHIN CODE: 1.7.1; jQueryUI version: 1.8.18" -->
<!-- *** But what I want is WITHIN CODE: 1.10.2; jQueryUI version: 1.10.4" -->
})
};
addAutocomplete('foo');
</script>
<script language="javascript" type="text/javascript">
// var rnSDJQ = jQuery;
window.jQuery = oldJQuery;
window.$ = oldDollar;
alert('AFTER jQuery version: ' + $.fn.jquery + '; jQueryUI version: ' + $.ui.version);
</script>
<!-- End of MY application -->
<!-- From this point, I cannot make changes to the code - is third-party code which could include jQuery/jQueryUI code potentially -->
</body>
</html>
That method seems very similar to this but I think that only applies to jQuery - jQueryUI apparently doesn't have a similar noConflict method.
2) Rewrite my application code to assume only early versions of jQuery and jQueryUI?
3) Make a copy of jQueryUI and do search-and-replace on (jquery)
as suggested here - sounds very nasty, and a maintenance nightmare!
4) Something like this? (Though I can't see how to apply it in my situation since I can't call noConflict on the first jQuery presumably?)
5) Some other option?
A further difficulty is that I can't find a way to include jQuery/jQueryUI conditionally - the third-party site might not actually be loading jQuery/jQueryUI so I need to cope with that possibility also.