通过location.assign()传递url哈希

Passing an url hash through location.assign()

本文关键字:传递 url 哈希 location assign 通过      更新时间:2023-10-15

我想通过url hash:向新页面传递一个参数

location.assign = 'display.php' + '#test';

但是,散列不会显示在新的display.php页面中。我尝试了location.hash(),但它只更改当前页面(home.php)的哈希,而不更改display.php的哈希。

我不知道这是否有用,但我正在与Wamp合作。所以最终的url看起来像http://localhost/tests/home.php

location.assign是一个方法,而不是属性。因此,应该这样调用:

location.assign('display.php#test');

根据文档,这应该尊重整个url(带有锚点)。

您可能需要使用location.href来设置位置:

location.href = 'display.php#test';

这通常是从JavaScript重定向浏览器的惯用方法。