Javascript history.PushState无法正常工作? [英] Javascript history.PushState not working?

查看:164
本文介绍了Javascript history.PushState无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有这个代码:

<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>

当我点击我的链接时,我在错误日志中收到此错误:

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'. 

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

you can see this at: http://cowelllaserhair.com/test.html

我做错了什么?

我需要引用一些东西吗?

Do I need to reference something?

谢谢,
J

Thanks, J

推荐答案

pushState 的URL参数应该相对于当前页面,或者是您自己域中的绝对URL。你不能推动州跨域 - 这将是一个重大的安全漏洞。

The URL argument to pushState should be relative to the current page, or or an absolute URL in your own domain. You can't push state cross-domain - it would be a major security flaw.

MDN文档说:


新历史记录条目的URL由此参数指定。请注意,在调用pushState()之后,浏览器不会尝试加载此URL,但稍后可能会尝试加载URL,例如在用户重新启动浏览器之后。新URL不一定是绝对的;如果是相对的,则相对于当前URL进行解析。新URL必须与当前URL的源相同;否则,pushState()将抛出异常。此参数是可选的;如果未指定,则将其设置为文档的当前URL。

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参数通常留空或设置相对您自己网站上的网址,例如#/ hello

The URL parameter is most commonly left blank or set a relative URL on your own site, like #/hello.

这篇关于Javascript history.PushState无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆