修改网址而不重新加载页面 [英] Modify the URL without reloading the page

查看:126
本文介绍了修改网址而不重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以在不重新加载页面的情况下修改当前页面的URL吗?



我想在部分>#散列如果可能的话。



我只需要改变域之后的部分,所以它不像我违反跨域域名政策。

  window.location.href =www.mysite.com/page2.php; //悲哀地是这会重新加载$ ​​b $ b  


解决方案

现在可以在Chrome,Safari,FF4 +和IE10pp4 +!

请参阅此问题的答案以获取更多信息:
使用新的URL更新地址栏,无需散列或重新加载页面



示例:

 函数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 来检测后退/前进按钮导航:

$ $ $ $ $ $ $ $ $ $> window.onpopstate = function(e){
if(e.state){
document.getElementById(content)。innerHTML = e.state.html;
document.title = e.state.pageTitle;
}
};






要更深入地了解操作浏览器历史记录,请参阅此MDN文章


Is there any way I can modify the URL of the current page without reloading the page?

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

解决方案

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

Example:

 function processAjaxData(response, urlPath){
     document.getElementById("content").innerHTML = response.html;
     document.title = response.pageTitle;
     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
 }

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;
    }
};


For a more in-depth look at manipulating browser history see this MDN article.

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

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