ink*_*dmn 8 javascript jquery compatibility
我正在为自己编写一个JavaScript工具,我计划向其他人提供它并使用jQuery.我对这个小实用程序的梦想是让人们能够.js
从远程源中包含一个文件,并且在加载该文件时,让它检查是否已经包含jQuery,如果有,则确保它是一个最近的版本,以便与我的代码需要做的兼容.一些伪代码可以更清楚地解释我的问题(这段代码会出现在.js
我之前提到的单个文件的顶部):
if jQuery is loaded {
if version is less than (say) 1.3 {
// load latest version of jQuery
}
} else {
// load latest version of jQuery
}
// and do the jQuery.noConflict()
// dance to avoid trampling any other libraries, etc. that the
// user may be utilizing.
// The rest of my code lives here, happy in the knowledge that it's running in
// isolation from the rest of the JS that the page knows about.
Run Code Online (Sandbox Code Playgroud)
我的问题,分为三个友好的部分:
这里的首要想法是,任何旧用户都可以抓取一小段HTML并将其放入他们的网站/博客/其他任何内容并使用Just Work™.由于许多现代出版平台现在都附带了jQuery,我不能只是静静地假设它没有运行并包含它.
感谢您的时间,如果我的任何部分不清楚,或者我可以提供额外的背景/细节,让您更轻松地提供回复,请告诉我.
Jam*_*mes 15
function doMyStuff(jQueryJustForThisFn) {
// do jQuery stuff!
jQueryJustForThisFn('div').addClass('wow');
}
function check() {
return window.jQuery && jQuery.fn && /^1\.[3-9]/.test(jQuery.fn.jquery);
}
if ( check() ) {
doMyStuff( jQuery );
} else {
var script = document.createElement('script'),
timer = setInterval(function(){
if ( check() ) {
clearInterval(timer);
document.body.removeChild(script);
doMyStuff( jQuery.noConflict(true) );
}
}, 30);
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js';
document.body.insertBefore( script, document.body.firstChild );
}
Run Code Online (Sandbox Code Playgroud)