如何仅更改页面URL

时间:2019-12-01 00:13:35

标签: javascript jquery html

我想知道如何在jQuery 中将页面URL 从example.com/file1.html?i=1更改为example.com/file1.html 更改页面内容重新加载页面

1 个答案:

答案 0 :(得分:0)

将更改URL而无需重定向或重新加载到file1.html。如果历史记录推送状态不可用,请使用替代方法。

if (history.pushState) {
  window.history.pushState("{ Object }", "Title", "file1.html");
} else {
  document.location.href = "file1.html";
}

功能

function Change(domain, title) {
    let url = domain + 'file1.html';
    window.history.pushState('data', `{$title}`, url);
    document.title = title;
}