【问题标题】:History replaceState no longer working in Chrome for local file历史 replaceState 不再在 Chrome 中为本地文件工作
【发布时间】:2015-09-04 12:52:00
【问题描述】:

我正在使用 window.history.replaceState() 来更改使用 file:///C:/... 访问的 HTML 文件的查询字符串...

这曾经在 Chrome、Internet Explorer 和 FireFox 中有效,但在 Chrome 中不再有效。不知道它什么时候停止工作。我有 Chrome 45.0.2454.85。它仍然适用于其他 2 个浏览器。

我得到:

未捕获的安全错误:无法在“历史记录”上执行“replaceState”: 带有 URL 的历史状态对象 'file:///C:/Users/Michael/Desktop/test.html?Q=NewQueryString' 不能 在原点为“null”的文档中创建。

抱歉,这在任何托管小提琴中都不起作用。您可以将代码复制到本地 HTML 文件中以自己查看:

<html>
<body>
<button onclick="clickme()">Click me</button>
<script>
function clickme() {
    window.history.replaceState({ "html": undefined, "pageTitle": "NewTitle"}, "", "?Q=NewQueryString");
}
</script>
</body>
</html>

显然我可以将它托管在服务器上,但我试图保持简单。这是我们使用的由 Git 托管的简单 Scrum 板。它通过第 3 方 Web 服务获取数据。有关解决该错误的任何想法。

【问题讨论】:

  • 什么版本,因为对我来说那是工作
  • Chrome 45.0.2454.85 不适合我。
  • 这搞砸了。我在 Chrome 中将我所有的应用程序绘制为本地 HTML 文件。很遗憾,由于 jQuery Mobile 需要 replaceState(),这不再有效。

标签: javascript google-chrome


【解决方案1】:

它停止在 Chrome 45 上运行。 在这里,我在 code.google 上填写了一个问题: https://code.google.com/p/chromium/issues/detail?id=529313

如果您确实只需要在自己的计算机上运行它,那么设置 --allow-file-access-from-files 是一个很好的解决方案。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。作为一种解决方法,我使用 --allow-file-access-from-files 标志启动 Chrome。

    但认为这不是最好的解决方案。

    以下是我的运行示例:

    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --app=file: ///C:/APP/index.html --allow-file-access-from-文件

    如果有人找到更好的解决方案,请告诉我们。

    【讨论】:

    • 如何在 Windows 上运行它?开始 => 运行 => "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --app=file:///C:/Users/Test/Desktop/test.html --allow -file-access-from-files 让我找不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2020-11-16
    • 2016-02-05
    相关资源
    最近更新 更多