且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不重新加载页面的情况下修改URL?

更新时间:2022-11-04 12:56:31

现在可以在Chrome,Safari,Firefox 4+和Internet Explorer 10pp4 +中完成!

This can now be done in Chrome, Safari, Firefox 4+, and Internet Explorer 10pp4+!

有关更多信息,请参见此问题的答案: 用新的地址栏更新不带哈希值或不重新加载页面的URL

See this question's answer for more information: 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;
    }
};


要更深入地了解操纵浏览器历史记录,请参见 查看全文