如何以编程方式删除Javascript引用

时间:2014-05-06 12:04:00

标签: c# asp.net master-pages

我有一个母版页,我在其中使用了scriptmanager并为其添加了外部js。 在我的子页面中,我想要一些其他js文件,这些文件与母版页中的js(例如Jquery插件)文件冲突。有没有办法从该特定子页面的母版页中删除脚本引用。我也尝试过scriptmanagerproxy,但仍然存在冲突。

2 个答案:

答案 0 :(得分:0)

您可以使用jQuery脚本来操作和删除链接:

<script type="text/javascript">
    $(document).ready(function () {
       $("link[type='text/css']").remove(); 
         // or
       $("link[href='Styles/Site.css']").remove();
    });
</script>

答案 1 :(得分:0)

我建议在母版页中添加一个属性

    private bool addScriptMasterPage = true;
    public bool AddScriptMasterPage
    {
        get
        {
            return addScriptMasterPage;
        }
        set
        {
            addScriptMasterPage = value;
        }
    }

然后在页面中,您不希望从母版页加载脚本,请使用

    MasterPage master = (MasterPage)this.Master;
    MasterPage.AddScriptMasterPage = false;

并且只有当属性为true时,我才加载主页面注册脚本。

Page.ClientScript.RegisterClientScriptInclude("key", "url.js")

这不是一种javascript方式,但可以以其他方式工作