代码之家  ›  专栏  ›  技术社区  ›  dotNET

bootstrap studio中特定于页面的脚本

  •  1
  • dotNET  · 技术社区  · 8 年前

    (此问题特定于bootstrap studio)

    我需要包含一段JS,它只针对一个页面。

    • 我不能把它放在JS文件中,因为它是特定于一个页面的,而且BSS显然没有将JS文件链接到单个页面的选项。它总是导入所有页面中的所有JS文件(我认为这本身就是一个严重的限制)。
    • 我不能把它放在页面中的自定义代码块中,因为它使用jquery,它在整个页面内容(包括 jQuery.js )所以我看到了 $未定义 运行脚本时出错。

    我的出路是什么?

    1 回复  |  直到 8 年前
        1
  •  1
  •   PierreN    8 年前

    您有:

    • 1)要等待jquery加载,可以使用以下代码执行此操作:

      $( document ).ready(function() {
        // Handler for .ready() called.
        if (window.location.pathname == '/mypage.php') {
          // execute my custom code
        }
      });
      

    或

        var waitForJQuery = setInterval(function () {
        if (typeof $ != 'undefined') {    
            // place your code here.
            if (window.location.pathname == '/mypage.php') {
              // execute my custom code
            }
    
           clearInterval(waitForJQuery);
            }
        }, 50);
    
    • 2)如果您只想在自定义页面上执行代码,可以检查您所在的页面,如果您在良好页面上,则可以执行自定义代码。

      if (window.location.pathname == '/mypage.php') {
        // execute my custom code
      }
      

      查看更多可能性: Best way to execute js only on specific page