JavaScript で相対 URL にリダイレクトする 質問する

JavaScript で相対 URL にリダイレクトする 質問する

私の問題は、JavaScript 経由で上のディレクトリにリダイレクトしたいということです。

私のコード:

location.href = (location.href).substr(0, (location.href).lastIndexOf('folder'))

URLは次のようになります:

example.com/path/folder/index.php?file=abc&test=123&lol=cool

リダイレクトは次のような影響を及ぼします:

example.com/path/&test=123&lol=cool

しかし、これが欲しいです:

example.com/path/

どうすればいいですか?

ベストアンサー1

相対リダイレクトを行うことができます:

window.location.href = '../'; //one level up

または

window.location.href = '/path'; //relative to domain

おすすめ記事