【问题标题】:JavaScript Page Not Redirecting ProperlyJavaScript 页面未正确重定向
【发布时间】:2015-01-28 22:07:04
【问题描述】:

我正在尝试使用来自 URL 的参数并使用 JavaScript 访问为 user_profile.html 创建一个页面系统。

但是,由于某种原因,我的页面没有使用# 参数刷新。我不确定为什么会这样。我已经把我的重定向函数和输出放在下面。

代码:

function pageRedirect(page) {
    var url = window.location.href.replace('#'+window.location.hash.substr(1), '#'+page);
    console.log("@@@@");
    console.log(url);
    console.log("@@@@");
    window.location.href = url;
}

Chrome 控制台输出:

@@@@ user_profile.js:296 http://**********/user_profile.html#1 user_profile.js:297 @@@@ Navigated to http://**********/user_profile.html

即使 URL 被更改为最后带有 #1 的相同内容,它也不会使用该参数进行刷新。

【问题讨论】:

  • 更改位置哈希不会导致页面刷新。这是故意的。不过,您可以监视 onhashchange 事件并手动重新加载页面
  • 那么解决办法是什么?
  • 改位置后试试window.location.reload()
  • 我试过你所说的 Sucker,但它产生了相同的结果。
  • 使用 location.href 和正确的 url:location.href = url 附加一个随机值以避免缓存。

标签: javascript html redirect


【解决方案1】:

更改网址哈希的最简单方法是:

window.location.hash = page; // not the URL, just the '#something' part without the '#'

如果您需要在此之后重新加载页面,您可以这样做:

window.location.reload();

【讨论】:

  • 嗯。使用它会产生同样的问题:var url = window.location.href.replace('#'+window.location.hash.substr(1), '#'+page); window.location.hash = url; window.location.reload();
猜你喜欢
  • 2017-01-18
  • 2011-08-11
  • 2018-06-08
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多