JavaScript页面未正确重定向
我正在尝试使用URL中的一个参数并使用JavaScript访问,为JavaScript页面未正确重定向,javascript,html,redirect,Javascript,Html,Redirect,我正在尝试使用URL中的一个参数并使用JavaScript访问,为user\u profile.html创建一个页面系统 但是,由于某些原因,我的页面没有使用参数刷新。我不知道为什么会这样。我把重定向函数和输出放在下面 代码: function pageRedirect(page) { var url = window.location.href.replace('#'+window.location.hash.substr(1), '#'+page); console.log(
user\u 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控制台输出:
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;
}
@@@@
user_profile.js:296 http://***********/user_profile.html#1
user_profile.js:297@@@@
导航到http://*********/user_profile.html
即使URL在最后用
#1
更改为相同的内容,它也不会用该参数刷新。更改URL哈希的最简单方法是:
window.location.hash = page; // not the URL, just the '#something' part without the '#'
如果在此之后需要重新加载页面,可以执行以下操作:
window.location.reload();
更改位置哈希不会导致页面刷新。这是故意的。您可以监视
onhashchange
事件并手动重新加载页面,但解决方案是什么?更改位置后,请尝试window.location.reload()
我尝试了您所说的,但它会产生相同的结果。请将location.href与正确的url一起使用:location.href=url
附加一个随机值以避免缓存.Hmm。使用此选项会产生相同的问题:var url=window.location.href.replace(“#”+window.location.hash.substr(1),“#”+page);window.location.hash=url;window.location.reload()代码>