回答

收藏

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

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

有没有办法在不重新加载页面的情况下修改当前页面的 URL?( K, W. H% A& @3 s  L# f' f- u* h
如果可能的话,我想去# hash前一部分。% J. z' G: d- p) n/ e9 x  [
我只需要更改域之后因此,我不违反跨域战略。" z+ A2 \1 j( {: _- ]& }/ Y, l' b
    window.location.href = "www.mysite.com/page2.php";  // Sadly this reloads
    2 U- G4 L# N& C/ G! u$ P
               
+ M0 t1 q2 n  e% z5 `. n! C    解决方案:                                                                ! F8 E! |7 S# n7 K. ~# z& {; }
                                                                现在可以在 Chrome、Safari、Firefox 4  和 Internet Explorer 10pp4  中完成!
- j0 F9 S, ?0 ~# L+ s7 F, ~" O4 _# K例子:9 U6 \8 Z) W% {8 Q7 y6 U8 L
    function processAjaxData(response,urlPath){     document.getElementById("content").innerHTML = response.html;     document.title = response.pageTitle;     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"",urlPath); }3 b, \! r0 \  Q7 `' N- m2 L
然后,你可以用它window.onpopstate检测后退/前进按钮导航:+ f2 s8 D4 |' y; k- W2 x8 h0 U
[code]window.onpopstate = function(e){    if(e.state){          document.getElementById("content").innerHTML = e.state.html;        document.title = e.state.pageTitle;   }code]
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则