仅在某些 ASPX 页上动态加载 jQuery 库

loading jquery libraries dynamically only on certain aspx pages

本文关键字:动态 加载 jQuery ASPX      更新时间:2023-09-26

由于特定原因。我在表单标签中有我的jquery引用,该标签位于正文html标签中,而不是头部html标签中。

到目前为止一切正常,但是由于我如何使用我的代码,jquery 库加载在每个页(ASPX 页)。

我想只在需要它们的页面上加载 jquery 引用。有没有办法我能做到?

这是我目前的母版页代码:

<body>
    <form id="form2" runat="server">
    <asp:ScriptManager ID="ScriptManagerService" runat="server">
        <Scripts>
            <asp:ScriptReference Path="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ScriptMode="Auto" />
            <asp:ScriptReference Path="http://code.jquery.com/jquery-migrate-1.2.1.js" ScriptMode="Auto" />
            <asp:ScriptReference Path="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js" ScriptMode="Auto" />
        </Scripts>
    </asp:ScriptManager>
   </form>
</body>

在母版页中删除对 jQuery 的脚本引用,并将引用 jQuery 的 ScriptManagerProxy 添加到需要它们的页面:

<body>
    <form id="form2" runat="server">
        <asp:ScriptManagerProxy ID="ScriptManagerService" runat="server">
            <Scripts>
                <asp:ScriptReference Path="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ScriptMode="Auto" />
                <asp:ScriptReference Path="http://code.jquery.com/jquery-migrate-1.2.1.js" ScriptMode="Auto" />
                <asp:ScriptReference Path="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js" ScriptMode="Auto" />
            </Scripts>
        </asp:ScriptManagerProxy>
    </form>
</body>