本文介绍了replaceState():具有url的历史状态...无法在具有origin的文档中创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have window.history.replaceState(null, null, 'about'); in main.js which are located in required/javascripts on my server.

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

Then on the about page (located in / (root) on my server), I have a link that uses window.history.replaceState(null, null, 'about:me'); on this page. Everything works fine, but when I click on another link with the same function but with about:girlfriend as URL, I'm getting this error message:

未捕获的SecurityError:无法在历史记录上执行pushState:无法在具有原点'http'的文档中创建URL为about:girlfriend'的历史状态对象://my.domain.com'。

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

I don't know why my browser (latest version of Chrome) think I'm trying to reach this page using pushState and I don't know why I'm getting this error message, no matter how many times I've read it. Can someone please explain this for me? I do not use History.js for this.

值得一提的是,我没有收到此错误如果我将更改为其他内容,例如 - / 。我想使用,因为 / 无效(404找不到页面)和 - 不适合 - 更好:

Worth mention, is that I'm not getting this error message if I change : to something else, like - or /. I want to use : because / is not working (404 Page Not Found) and - doesn't fit in - it's better with :.

推荐答案

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

If you are trying to do this locally, the following works for both locally and remote loaded pages:

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

这篇关于replaceState():具有url的历史状态...无法在具有origin的文档中创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 19:46