简体   繁体   English

Javascript history.PushState无法正常工作?

[英]Javascript history.PushState not working?

I have this code here: 我在这里有这个代码:

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

and

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

when I click on my link, I get this error in my error log: 当我点击我的链接时,我在错误日志中收到此错误:

 Uncaught SecurityError: A history state object with URL 'http://www.google.ca/' cannot be created in a document with origin 'http://cowelllaserhair.com'. 

you can see this at: http://cowelllaserhair.com/test.html 你可以在http://cowelllaserhair.com/test.html看到这个

What am I doing wrong? 我究竟做错了什么?

Do I need to reference something? 我需要参考一下吗?

Thanks, J 谢谢,J

The URL argument to pushState should be relative to the current page, or or an absolute URL in your own domain. pushState的URL参数应该相对于当前页面,或者是您自己域中的绝对URL。 You can't push state cross-domain - it would be a major security flaw. 你不能推动州跨域 - 这将是一个重大的安全漏洞。

The MDN documentation says: MDN文档说:

The new history entry's URL is given by this parameter. 新历史记录条目的URL由此参数给出。 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. 请注意,在调用pushState()之后,浏览器不会尝试加载此URL,但稍后可能会尝试加载URL,例如在用户重新启动浏览器之后。 The new URL does not need to be absolute; 新URL不一定是绝对的; if it's relative, it's resolved relative to the current URL. 如果是相对的,则相对于当前URL进行解析。 The new URL must be of the same origin as the current URL; 新网址必须与当前网址的来源相同; otherwise, pushState() will throw an exception. 否则,pushState()将抛出异常。 This parameter is optional; 此参数是可选的; if it isn't specified, it's set to the document's current URL. 如果未指定,则将其设置为文档的当前URL。

The URL parameter is most commonly left blank or set a relative URL on your own site, like #/hello . URL参数通常留空或在您自己的站点上设置相对URL,例如#/hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM