0

リンク/URL/アドレスに移動する方法を知っています。

window.location = "www.example.com/index.html";

また

document.location.href="www.example.com/index.html";

index1.htmlしかし、 からにナビゲートしたい場合、接頭辞index2.htmlを提供せずにこれを実現するにはどうすればよいでしょうか? グローバル変数/定数www.example.com/に設定することを提案しないでください。www.example.com/アドレスは ... に変更さwww.example2.com/subfolder1/subfolder2/れる場合があります。www.examplea.com/前述の方法は、ルートページの場合にのみ機能します。つまり、にとどまっている場合でも、を提供するdocument.location.href="index.html";とブラウザが に移動します。しかし、私はに移動したいrootdomain/index.htmlrootdomain/section1/section2/somepage.htmlrootdomain/section1/section2/index.html

ページ名だけを指定してこれを達成するにはどうすればよいですか?

4

3 に答える 3

1

文字列の先頭にa がある場合/、ローカル ページに移動します。

window.location = "/index.html"
于 2013-03-30T15:31:08.850 に答える
0

window.location.pathname = 'index2.html';

で相対 URL を使用することもできますdocument.location.href

さらに良いのはwindow.location.assign('index2.html');. これは、www.example.com/foo/.../index1.html にいて、www.example.com/foo/.../index2 に到達するパス全体を指定したくない場合に特に便利です。 .html.

その他のオプションについては、https://developer.mozilla.org/en-US/docs/DOM/window.locationを参照してください。

于 2013-03-30T15:30:50.473 に答える
0

他の方法と同じですが、相対パスを使用します。

document.location.href = '/index2.html'; //relative path
于 2013-03-30T15:31:06.603 に答える