代码之家  ›  专栏  ›  技术社区  ›  Elaine Byene

仅在附加了的特定URL上执行javascript

  •  -1
  • Elaine Byene  · 技术社区  · 7 年前

    我必须在URL中带有哈希标记的特定页面上执行JS。我只需要在产品2上执行JS,然后隐藏在其他人身上。

    我的URL模式是 /about#product1 , /about#product2 , /about#product3 等和JS代码:

    <script type="text/javascript">
      if( window.location.href.indexOf('/about#product2') !== -1 ) {
       alert('Hello');
    }
    </script>
    

    我该怎么办?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Elaine Byene    7 年前

    对于寻找答案的人:

    <script type="text/javascript">
      if (window.location.href.match(/\#product2/))
        {
          alert('hello');
        }
    </script>