【发布时间】: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