3

ユーザーがサイトのルート URL にアクセスしている場合に js スクリプトを実行したいと考えています。私はこのコードを使用しています:

<script type="text/javascript">
    if(location.pathname == "/") {
        window.open ('#107','_self',false);
    }
</script>

ただし、パス名にはクエリ文字列とロケーション ハッシュが含まれていないため、http://example.com/?foo=bar# hash もスクリプトを実行します。

現在、次のように動作します。

http://example/                  Root
              /?querystring      Root
              /#hash             Root
              /page              Not root

私はそれが次のように動作することを望みます:

http://example/                  Root
              /?querystring      Not root
              /#hash             Not root
              /page              Not root

助言がありますか?ありがとう。

4

3 に答える 3

4

window.location.href代わりに使用

于 2012-08-27T11:00:39.317 に答える
3

パス名、location.search、および location.hash を連結できます。

var fullPath = location.pathname + location.search + location.hash;
if(fullPath == "/") { /* whatever */ }
于 2012-08-27T11:02:51.787 に答える
2

3つのコンポーネントすべてをテストできます。

<script type="text/javascript">
    if(location.pathname == "/" && 
        location.hash.length <= 1 && 
        location.search.length <= 1) {
        window.open ('#107','_self',false);
    }
</script>
于 2012-08-27T11:45:16.407 に答える