旧行李 2009-05-05 10:54 采纳率: 25%
浏览 425
已采纳

修改 URL 而无需重新加载页面

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

转载于:https://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page

  • 写回答

19条回答 默认 最新

  • local-host 2010-07-28 15:30
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(18条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功