回答

收藏

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

技术问答 技术问答 272 人阅读 | 0 人回复 | 2023-09-12

有没有办法在不重新加载页面的情况下修改当前页面的 URL?9 \1 f1 P4 l2 K/ t! W8 O
如果可能的话,我想去# hash前一部分。/ k( L7 ~) o, m6 I
我只需要更改域之后因此,我不违反跨域战略。. E  i5 c! z3 X
    window.location.href = "www.mysite.com/page2.php";  // Sadly this reloads
    + O' E( r' `5 |3 l( N% ]
               
% P( g  t9 J1 ~7 O! N" V5 x    解决方案:                                                               
& }7 i: ^: \# d  N3 k                                                                现在可以在 Chrome、Safari、Firefox 4  和 Internet Explorer 10pp4  中完成!
% [: X) {5 |( t例子:
, r& M; O  g* ~! r& R+ J) k
    function processAjaxData(response,urlPath){     document.getElementById("content").innerHTML = response.html;     document.title = response.pageTitle;     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"",urlPath); }+ Q5 `2 c# s* [, o
然后,你可以用它window.onpopstate检测后退/前进按钮导航:
: n& o' x( Y: w& r/ j[code]window.onpopstate = function(e){    if(e.state){          document.getElementById("content").innerHTML = e.state.html;        document.title = e.state.pageTitle;   }code]
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则