【问题标题】:Change complete URL in adress bar, without reloading the page更改地址栏中的完整 URL,无需重新加载页面
【发布时间】:2016-06-08 01:24:27
【问题描述】:

我需要一个网页在访问时完全改变用户地址栏。

例如:我访问 www.X.com,但我的地址栏显示 www.Z.com。

我已经查看了 history.pushState / history.replaceState,但使用它们我得到的结果如下:www.X.com/www.Z.com。

有没有办法,最好是使用history.pushState/replaceState,来实现这个效果?

【问题讨论】:

标签: javascript html


【解决方案1】:
function changeUrl(page, url) {
    if (typeof (history.pushState) != "undefined") {
        var obj = { Page: page, Url: url };
        history.pushState(obj, obj.Page, obj.Url);
    } else {
        alert("Browser does not support HTML5.");
    }
}
$(function () {
    $("#button1").click(function () {
        changeUrl('Page1', 'Page1.htm');
    });
    $("#button2").click(function () {
        changeUrl('Page2', 'Page2.htm');
    });
    $("#button3").click(function () {
        changeUrl('Page3', 'Page3.htm');
    });
});

【讨论】:

  • 最好能解释一下代码的作用,而不是仅仅放置一段代码。
【解决方案2】:

我通过托管服务提供商解决了这个问题。

【讨论】:

  • 这不是问题的答案。这个问题的实际答案是,出于安全原因,在不重新加载页面的情况下更改浏览器栏中的主机名是不可能的。
猜你喜欢
  • 2013-03-20
  • 2012-06-12
  • 2020-06-20
  • 2012-08-23
  • 2016-01-12
  • 1970-01-01
  • 2012-11-05
  • 2016-02-01
  • 1970-01-01
相关资源
最近更新 更多