在不重新加载页面的情况下更改浏览器地址栏中的URL

3
我希望能够在不重新加载页面的情况下更改浏览器地址栏中的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 后,浏览器地址栏中的 URL 更改为 www.b-site.com,当用户刷新页面时,它可以自动重定向到 www.b-site.com 吗?这是可能的吗?
谢谢...............

使用iframe来实现此功能。 - PW Kad
它们是真正的两个不同的网站还是同一域名下的不同页面? - Yuriy Galanter
这是两个不同的网站。 - Axeem
1个回答

11

你正在寻找以下内容

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

以下是我阅读的原始文章(发布于2012年7月10日):HTML5:在不刷新页面的情况下更改浏览器URL。

示例:

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

文档


不支持跨域。 - Yuriy Galanter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接