【问题标题】:How to add Jquery cdn in ScriptManager and if the load fail then load from local file如何在 ScriptManager 中添加 Jquery cdn,如果加载失败则从本地文件加载
【发布时间】:2012-01-16 12:57:03
【问题描述】:

在 asp:ScriptManager 中我像这样加载 jquery。

<asp:ScriptManager ID="ScriptManagerMasterPage" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/Scripts/jquery-1.6.4.min.js" />
        <asp:ScriptReference Path="~/Scripts/jquery-ui-1.8.16.min.js" />
        <asp:ScriptReference Path="~/Scripts/tag-it.js" />
        <asp:ScriptReference Path="~/Scripts/MosJScript.js" />
        <asp:ScriptReference Path="~/Scripts/jquery.tools.min.js" />
        <asp:ScriptReference Path="~/Scripts/jquery.dragsort.js" />
        <asp:ScriptReference Path="~/Scripts/jquery.ad-gallery.pack.js" />
    </Scripts>
</asp:ScriptManager>

假设我想使用来自 google 的 jquery cdn,但如果 cdn 文件无法加载,那么我将从本地机器加载。如何做到这一点。

【问题讨论】:

  • 我真的很想知道您是否想通过 HTML 中的代码来执行此操作...当 CDN 确实失败时,页面的加载时间可能会很糟糕 - 想象一下如果 CDN 很慢并且需要很长时间才能超时,它会阻止页面,直到每个元素的超时时间过去...如果您不能信任 CDN,请使用不同的 CDN,即您可以信任的 CDN。

标签: jquery asp.net cdn scriptmanager


【解决方案1】:

我认为您正在寻找类似于this 的内容。搜索词是“后备”文​​件。

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

【讨论】:

  • 谢谢,看来我们不能在 ScriptManager 中使用它,对吧?
  • 你总是可以把这段脚本放在一个脚本文件中,并将它包含在 ScriptManager 中。注意:我对 ScriptManager 的执行顺序知之甚少,不知道它是否尊重顺序。
【解决方案2】:

您必须添加一个脚本资源映射定义。例如,请参阅下面的文档:

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.scriptmanager.scriptresourcemapping

然后你必须将ScriptManager的EnableCdn属性设置为true,才能使用CDN。

【讨论】:

    猜你喜欢
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2013-09-08
    • 2016-08-26
    • 2019-11-13
    • 2012-11-06
    相关资源
    最近更新 更多