重定向到同一页面以锚定

v2p*_*v2p 14 php redirect

需要从http://sitename.com/cat/index.php重定向到http://sitename.com/cat/index.php#anchor

我尝试了两种方法:php重定向和元刷新

PHP重定向

<?php
    $URL = "http://sitename.com/cat/index.php#anchor";
    header("Location: $URL");
?>
<html>
....
Run Code Online (Sandbox Code Playgroud)

元刷新

<html>
    <head>
    ... 
    <meta http-equiv="refresh" content="0;url=http://sitename.ru/cat/index.php#anchor">
    ...
    </head>
...
</html>
Run Code Online (Sandbox Code Playgroud)

结果两种方式我都有自行车页面刷新.

如何正确解决这个问题?

xda*_*azz 9

您无需重定向页面,只需设置window.location.hash.

window.location.hash="anchor";
Run Code Online (Sandbox Code Playgroud)

  • @xdazz是否有可能在不使用javascript的情况下解决这个问题? (3认同)