gpt4 book ai didi

Javascript history.PushState 不起作用?

转载 作者:行者123 更新时间:2023-12-03 13:28:25 24 4
gpt4 key购买 nike

我在这里有这段代码:

<script type="text/javascript">
function goFunction(){
history.pushState("google.ca", "GOOGLE CANADA", "http://www.google.ca");
return event.preventDefault();
}
</script>


<a href="#" onclick="javascript:goFunction();">GO</a>

当我单击我的链接时,我的错误日志中出现此错误:
 Uncaught SecurityError: A history state object with URL 'http://www.google.ca/' cannot be created in a document with origin 'http://cowelllaserhair.com'. 

您可以在以下位置查看: http://cowelllaserhair.com/test.html

我究竟做错了什么?

我需要引用什么吗?

谢谢,
Ĵ

最佳答案

pushState 的 URL 参数应该是相对于当前页面的,或者是您自己域中的绝对 URL。您不能跨域推送状态 - 这将是一个主要的安全漏洞。

MDN documentation说:

The new history entry's URL is given by this parameter. Note that the browser won't attempt to load this URL after a call to pushState(), but it might attempt to load the URL later, for instance after the user restarts her browser. The new URL does not need to be absolute; if it's relative, it's resolved relative to the current URL. The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception. This parameter is optional; if it isn't specified, it's set to the document's current URL.



URL 参数通常留空或在您自己的网站上设置相对 URL,例如 #/hello .

关于Javascript history.PushState 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20079704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com