如何在不重新加载页面的情况下修改URL?
有没有办法在不重新加载页面的情况下修改当前页面的URL?
Is there any way I can modify the URL of the current page without reloading the page?
我想访问之前部分> #hap如果可能的话。
I would like to access the portion before the # hash if possible.
我只需要更改域之后的部分,所以它不像我违反了域名政策。
I only need to change the portion after the domain, so its not like I'm violating cross-domain policies.
window.location.href = "www.mysite.com/page2.php"; // sadly this reloads
现在可以在Chrome,Safari,FF4 +和IE10pp4 +!
This can now be done in Chrome, Safari, FF4+, and IE10pp4+!
有关详细信息,请参阅此问题的答案:
使用新网址更新地址栏,无需哈希或重新加载页面
See this question's answer for more info: Updating address bar with new URL without hash or reloading the page
示例:
function processAjaxData(response, urlPath){
document.getElementById("content").innerHTML = response.html;
document.title = response.pageTitle;
window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
}
然后你可以使用 window.onpopstate
检测后退/前进按钮导航:
You can then use window.onpopstate
to detect the back/forward button navigation:
window.onpopstate = function(e){
if(e.state){
document.getElementById("content").innerHTML = e.state.html;
document.title = e.state.pageTitle;
}
};
有关操纵浏览器的更深入了解历史见此MDN文章。