JavaScript 页面未正确重定向

JavaScript Page Not Redirecting Properly

本文关键字:重定向 JavaScript      更新时间:2023-09-26

我正在尝试创建一个页面系统,用于使用 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;
}

铬控制台输出:

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

即使 URL 更改为相同的内容,末尾带有#1,也不会使用该参数刷新。

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

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

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

window.location.reload();