这个问题的后续内容如下: Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail
所以我可以检测JQuery CDN是否已关闭并允许这样做:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='/Script/jquery-1.7.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>
如果加载为:
,如何对jquery.validate执行相同的操作<script type="text/javascript" language="javascript" src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js"></script>
答案 0 :(得分:10)
请改用:
if(typeof $().validate == 'undefined')
{
/// same code as before except pointing at your local jQuery.validate path
}
如果存在,则在控制台中返回true,否则返回false 删除DEMO中的脚本引用以获取false作为输出。
您的完整代码可能与此类似:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='/Script/jquery-1.7.2.min.js' type='text/javascript'%3E%3C/script%3E"));
};
if (typeof $().validate == 'undefined') {
document.write(unescape("%3Cscript src='/Script/jquery.Validate-1.6.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>
答案 1 :(得分:3)
这样的事情可以解决问题
<script type="text/javascript" language="javascript" src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js"></script>
<script type="text/javascript">
if (typeof jQuery().validate == 'undefined') {
document.write(unescape("%3Cscript src='/js/jquery.validate.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>