修改网址而不重新加载页面

编程入门 行业动态 更新时间:2024-10-26 18:18:00
本文介绍了修改网址而不重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

window.location.href =www.mysite/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/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.

更多推荐

修改网址而不重新加载页面

本文发布于:2023-10-12 21:00:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   加载   页面   网址

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!