javascript - 使用不同的 URI anchor 重新加载页面

标签 javascript php jquery

这是我的问题的简化 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/

相关文章:

javascript - 为什么我必须在选定的选项上单击两次?

php - 如何控制php无限极分类的深度

javascript - 遍历java脚本中的不同文本框并赋值

javascript - 类型错误 : Cannot read property &#39;location&#39; of undefined

php - 使用哪个 : REMOTE_ADDR or SERVER_ADDR

php - codeigniter 中的 Assets

javascript - 使用 2 个数据属性定位 HTML 元素

javascript - 如何将 2 个项目推送到数组

jquery - Bootstrap - 以编程方式附加并显示弹出框到元素

javascript - ReactJS 中的 DOM 操作和页面重新加载