如何检查客户端机器上加载了jQuery的哪个版本?客户端可能有jQuery加载,但我不知道如何检查它。如果他们有加载,我如何检查版本和前缀,如:
$('.class')
JQuery('.class')
如何检查客户端机器上加载了jQuery的哪个版本?客户端可能有jQuery加载,但我不知道如何检查它。如果他们有加载,我如何检查版本和前缀,如:
$('.class')
JQuery('.class')
当前回答
你应该在IE的try/catch块中封装这个:
// Ensure jquery is loaded -- syntaxed for IE compatibility
try
{
var jqueryIsLoaded=jQuery;
jQueryIsLoaded=true;
}
catch(err)
{
var jQueryIsLoaded=false;
}
if(jQueryIsLoaded)
{
$(function(){
/** site level jquery code here **/
});
}
else
{
// Jquery not loaded
}
其他回答
我发现这是检查jQuery是否加载的最短和最简单的方法:
if (window.jQuery) {
// jQuery is available.
// Print the jQuery version, e.g. "1.0.0":
console.log(window.jQuery.fn.jquery);
}
http://html5boilerplate.com和其他公司使用了这种方法。
也许不言自明,但我发现的最简单的方法是查看实际文件中注释的版本(通常是jquery.min.js)。或者,如果使用类似code.jquery.com的东西,则版本号在https引用中(即https://code.jquery.com/jquery-1.11.3.js)。
在这里输入图像描述
if (jQuery){
//jquery loaded
}
…
我的偏好是:
console.debug("jQuery "+ (jQuery ? $().jquery : "NOT") +" loaded")
结果:
jQuery 1.8.0加载
在一行和最少的按键(哎呀!):
alert($().jquery);