【问题标题】:Would that be possible to add a JavaScript file to the page conditionally without using Server side code?是否可以在不使用服务器端代码的情况下有条件地将 JavaScript 文件添加到页面?
【发布时间】:2011-09-26 10:38:30
【问题描述】:

我有 2 个 javascript 文件; Project.js 和 Project-debug.js。

当我的 ASP.NET 在 DEBUG 模式下运行时,应该加载后者,而在 RELEASE 模式下,应该加载前者。

是否可以在 html 中执行类似的操作来添加我的脚本?

例如

<script src="iif(DEBUG,../Scripts/Project-debug.js,../Scripts/Project.js" type="text/javascript"></script>

【问题讨论】:

    标签: javascript asp.net html debug-symbols


    【解决方案1】:
    【解决方案2】:

    是的,可以做到:

    <script type="text/javascript">
        var fileref = document.createElement('script');
        fileref.setAttribute("type", "text/javascript");
        if (DEBUG) {  
           fileref.setAttribute("src", "Project-debug.js");
        } else {
           fileref.setAttribute("src", "Project.js");
        }
        document.getElementsByTagName("head")[0].appendChild(fileref);
    </script>
    

    在你的 head 标签中添加这个

    【讨论】:

      猜你喜欢
      • 2011-11-28
      • 2013-07-27
      • 1970-01-01
      • 2018-09-15
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      相关资源
      最近更新 更多