这是我的问题的简化 PHP 版本:
我要加载http://localhost/test2.php在浏览器中, 如果缺少 #test_test,请立即重新加载 http://localhost/test2.php#test_test .
<?php
// causes ERR_TOO_MANY_REDIRECTS
if (strpos(strtolower($_SERVER['REQUEST_URI']), 'test_test') === false) {
header("Location: /test2.php#test_test");
}
?>
简而言之,我想检查 URI 中的 anchor ,如果丢失,则使用它重新加载页面。我收到 ERR_TOO_MANY_REDIRECTS。关于如何解决这个问题有什么建议吗?
最佳答案
你可以这样使用js来做到这一点:
let currentUrl = window.location.href;
if (currentUrl.indexOf('#test_test') === -1) {
window.location.href = currentUrl.replace(/(#.+)/gi, '#_test_test');
}
关于javascript - 使用不同的 URI anchor 重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41654994/