如何在不重新加载页面的情况下修改URL? [英] How to modify the URL without reloading the page?

查看:133
本文介绍了如何在不重新加载页面的情况下修改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文章

这篇关于如何在不重新加载页面的情况下修改URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆