【问题标题】:Jquery Migrate CDN fallback conditionJquery Migrate CDN 回退条件
【发布时间】:2013-08-19 16:41:19
【问题描述】:

为了提高页面速度,我正在使用谷歌 CDN 下载 jquery 文件,我也回退到从本地下载 jquery,以防谷歌 CDN 失败。

下面是如何使用回退

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='common/script/jquery-1.9.1.min.js' type='text/javascript'%3E%3C/script%3E"));
}

</script>

以上代码完美运行,我的问题是如何检查 jquery 迁移文件是否已加载?

我只需要检查http://code.jquery.com/jquery-migrate-1.1.0.js 是否已加载。我希望 if (typeof jQuery == 'undefined') {} 这在这里不起作用。

任何解决方案

【问题讨论】:

    标签: jquery cdn google-cdn jquery-migrate


    【解决方案1】:

    您可以检查$.fn.live 方法或jQuery.migrateWarnings 对象是否存在

    <script type="text/javascript" src="http://code.jquery.com/jquery-migrate-1.1.0.min.js"></script>
    <script type="text/javascript">
    if (typeof jQuery.migrateWarnings == 'undefined') { // or typeof jQuery.fn.live == 'undefined'
        document.write(unescape("%3Cscript src='common/script/jquery-migrate-1.1.0.min.js' type='text/javascript'%3E%3C/script%3E"));
    }
    
    </script>
    

    【讨论】:

    • 任何指向 jQuery.migrateWarnings 或 $.fn.live 的参考链接?
    • no... 这些都是 hack,因为这些属性不会出现在 jQuery >= 1.9