回答

收藏

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

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

有没有办法在不重新加载页面的情况下修改当前页面的 URL?$ Y3 |* y" J9 F( B% S
如果可能的话,我想去# hash前一部分。
2 d8 m. N( x: L' V2 P! L5 m我只需要更改域之后因此,我不违反跨域战略。
+ }! g% z( a: T5 a5 ?
    window.location.href = "www.mysite.com/page2.php";  // Sadly this reloads4 g  N% V- I( ]8 e
               2 K6 B7 n+ h$ Z2 A
    解决方案:                                                               
8 S2 D( M6 W0 X. Y2 N0 k# P, C8 G8 R                                                                现在可以在 Chrome、Safari、Firefox 4  和 Internet Explorer 10pp4  中完成!& D4 o+ @, Y8 w: k# K5 {, |
例子:9 F; w( U$ ^: R2 z. |3 a, i+ ]6 z
    function processAjaxData(response,urlPath){     document.getElementById("content").innerHTML = response.html;     document.title = response.pageTitle;     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"",urlPath); }
    $ L5 `2 R3 H$ _% ?+ `
然后,你可以用它window.onpopstate检测后退/前进按钮导航:
% M) b, v% K6 W/ H[code]window.onpopstate = function(e){    if(e.state){          document.getElementById("content").innerHTML = e.state.html;        document.title = e.state.pageTitle;   }code]
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则