【问题标题】:Change the url in browser bar without reloading page [duplicate]更改浏览器栏中的网址而不重新加载页面[重复]
【发布时间】:2013-10-12 04:00:16
【问题描述】:

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

<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。这可能吗。
谢谢........

【问题讨论】:

  • 使用 iframe 来做到这一点
  • 它们真的是同一个域中的 2 个不同的站点或不同的页面吗?
  • 这是两个不同的站点。

标签: javascript url


【解决方案1】:

您正在寻找关注者

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

【讨论】:

  • 不能跨域工作。
猜你喜欢
  • 2012-06-12
  • 2012-08-23
  • 2011-08-02
  • 2013-04-25
  • 2015-02-14
  • 2020-06-20
  • 2014-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多