这个问题的一些后续内容: 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>
最佳答案
改用这个:
if(typeof $().validate == 'undefined')
{
/// same code as before except pointing at your local jQuery.validate path
}
DEMO - 检查验证是否存在
如果存在则在控制台中返回 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>
关于JQuery.Validate CDN 后备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564896/