更改浏览器栏中的网址而不重新加载页面

时间:2013-10-05 02:28:39

标签: javascript url

我想在不重新加载页面的情况下更改浏览器栏中的网址。

<html><head>
 <title>Change url in browser bar</title>
</head>
     <body>
             <h1>Refresh page to see our other website</h1>
     </body>
 </html>

当任何用户进入我的网站时 www.a-site.com 打开此网站后,他会看到文本刷新页面以查看我们的其他网站。进入 www.a-site.com 时,我可以在 www.b-site.com 等浏览器栏中更改此网址后以及用户刷新页面时执行此操作它可以自动重定向  的 www.b-site.com 即可。这是可能的。
  感谢...............

1 个答案:

答案 0 :(得分:11)

您正在寻找以下

window.history.pushState("object or string", "Title", "/new-url");

以下是我读过的原创文章(2012年7月10日发布):HTML5:Changing the browser-URL without refreshing page.

示例:

 function processAjaxData(response, urlPath){
     document.getElementById("content").innerHTML = response.html;
     document.title = response.pageTitle;
     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
 }

Documentation