JQuery.Validate CDN后备

Lia*_*iam 11 jquery cdn jquery-validate

从这个问题中得到一点: 使用谷歌托管的jQuery的最佳方式,但回到我在Google上的托管库失败

所以我可以检测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>
Run Code Online (Sandbox Code Playgroud)

如何加载为jquery.validate,我该如何做同样的事情:

<script type="text/javascript" language="javascript" src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

Nop*_*ope 10

请改用:

if(typeof $().validate == 'undefined')
{
    /// same code as before except pointing at your local jQuery.validate path
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)