【问题标题】:replaceState(): a history state with url ... cannot be created in a document with originreplaceState(): 带有 url ... 的历史状态不能在带有源的文档中创建
【发布时间】:2014-11-01 15:17:23
【问题描述】:

我在main.js 中有window.history.replaceState(null, null, 'about');,它们位于我的服务器上的required/javascripts

然后在关于页面(位于我的服务器上的/(根))上,我有一个在此页面上使用window.history.replaceState(null, null, 'about:me'); 的链接。一切正常,但是当我单击另一个具有相同功能但以about:girlfriend 作为 URL 的链接时,我收到以下错误消息:

Uncaught SecurityError: Failed to execute 'pushState' on 'History': A history state object with URL 'about:girlfriend' cannot be created in a document with origin 'http://my.domain.com'.

我不知道为什么我的浏览器(最新版本的 Chrome)认为我正在尝试使用 pushState 访问此页面,而且我不知道为什么我会收到此错误消息,无论多少次我已经读过了。有人可以为我解释一下吗?我不为此使用History.js

值得一提的是,如果我将: 更改为其他内容,例如-/,我不会收到此错误消息。我想使用:,因为/ 不工作(404 页未找到)并且- 不适合 - 最好使用:

【问题讨论】:

  • 你有没有解决过这个问题,因为我现在在我的项目中遇到了完全相同的问题。
  • Javascript history.PushState not working? 的可能副本。另外,请注意,如果您尝试在本地查看网页,则会收到此错误。该页面需要通过 HTTP 服务器才能工作,因此如果您只是从硬盘驱动器在浏览器中打开 index.html,您将收到此错误。

标签: html html5-history


【解决方案1】:

如果您尝试在本地执行此操作,则以下内容适用于本地和远程加载的页面:

history.replaceState(null,null, window.location.pathname + "your thing here")

【讨论】:

    猜你喜欢
    • 2018-02-05
    • 2018-08-03
    • 1970-01-01
    • 2020-11-16
    • 2018-07-02
    • 2015-12-05
    • 2016-02-04
    • 2013-01-25
    • 1970-01-01
    相关资源
    最近更新 更多