我認為您可以簡單地使用另一個別名來加載您的jquery,並讓插件使用通用別名$
。
通常是加載所有插件並在需要時使用它們。 所以if($(window).width() > 768) { // fire script }
聽起來不錯。 另一種方法是使用jQuery getScript()
方法,該方法允許您異步加載腳本:
(function(j){
if( $(window).width() > 768 ){
j.getScript('http://widgets.something.de/clientidblablabla?parameters=2324', function(){
// script loaded, you can play with it from here
});
}
})(jQuery);
編輯:
我舉了一個例子( jsFiddle )來更好地解釋它。
(function(j213){
// right now j213 and $ are aliases of the same version (2.1.3)
j213('body').append( 'j213 = ' + j213().jquery + '<br/>' );
$('body').append( '$ = ' + $().jquery + '<br/>' );
j213.getScript('https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js', function(){
// once another version of jQuery has been loaded, $ becomes an alias for 1.4.1 while j213 still holds for 2.1.3
$('body').append( '$ = ' + $().jquery + '<br />' );
// you can also remove an alias
delete $; // or $ = undefined;
// or $ = null;
// or $ = 'whatever you want';
j213('body').append(typeof($) + '<br />'); // returns undefined
// or assign a new one easily
$ = jQuery; // $ is now 1.4.1 - the last jQuery loaded
$('body').append( '$ = ' + $().jquery + '<br />' );
$ = j213; // $ is now 2.1.3
$('body').append( '$ = ' + j213().jquery + '<br />' );
});
})(jQuery);