无法使用Google代码在"DOMWindow"上执行"postMessage" [英] Failed to execute 'postMessage' on 'DOMWindow' using Google code

查看:70
本文介绍了无法使用Google代码在"DOMWindow"上执行"postMessage"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自己的网站上添加Google+徽章.当我从Google配置工具复制/粘贴代码时,徽章不起作用.我以为问题可能出在JavaScript中,所以我创建了一个空的index.html并复制/粘贴了Google代码...但仍然无法正常工作:

I would like to add a Google+ badge on my site. When I copy/paste code from Google configuration tool, the badge does not work. I thought the problem might be in the JavaScript so I created an empty index.html and copy/paste the Google Code... but it still does not work:

Google代码:

<!-- Place this tag in your head or just before your close body tag. -->
<script src="https://apis.google.com/js/platform.js" async defer></script>

<!-- Place this tag where you want the widget to render. -->
<div class="g-page" data-width="250" data-href="https://plus.google.com/110789175777197362579" data-theme="dark" data-rel="publisher"></div>

index.html中的完整HTML

FULL HTML in index.html

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>

<!-- Place this tag where you want the widget to render. -->
<div class="g-page" data-width="250" data-href="https://plus.google.com/110789175777197362579" data-theme="dark" data-rel="publisher"></div>

<!-- Place this tag in your head or just before your close body tag. -->
<script src="https://apis.google.com/js/platform.js" async defer></script>
</body>
</html>

在控制台中,我可以看到以下错误:

In the console I can see the following errors:

  • 无法在"DOMWindow"上执行"postMessage":提供的目标原点("null")与收件人窗口的原点("null")不匹配.人
  • 不赞成使用getAttributeNode().使用getAttribute()代替.rs = AGLTcCOasCY_GkJWPJtHt2mUGUYuzPzTCg%20line%209%20%3E%20eval:34:434
  • 无法在"DOMWindow"上执行"postMessage":提供的目标原点("null")与收件人窗口的原点("null")不匹配.人
  • 无法在"DOMWindow"上执行"postMessage":提供的目标原点("null")与收件人窗口的原点("null")不匹配.postmessageRelay
  • 无法在"DOMWindow"上执行"postMessage":提供的目标原点("null")与收件人窗口的原点("null")不匹配.postmessageRelay
  • 无法在"DOMWindow"上执行"postMessage":提供的目标原点("null")与收件人窗口的原点("null")不匹配.postmessageRelay
  • 无法在"DOMWindow"上执行"postMessage":提供的目标原点("null")与收件人窗口的原点("null")不匹配.postmessageRelay
  • 无法在"DOMWindow"上执行"postMessage":提供的目标原点("null")与收件人窗口的原点("null")不匹配.postmessageRelay
  • 无法在"DOMWindow"上执行"postMessage":提供的目标原点("null")与收件人窗口的原点("null")不匹配.postmessageRelay

推荐答案

问题可能是Google+徽章可能要求您通过网络服务器运行代码,而不是通过文件系统在本地查看代码.

The issue may be that the Google+ badge may require you to run the code through a web server instead of viewing it locally through the filesystem.

不只是打开HTML文件,而是尝试使用Web服务器托管它.这里有很多选择.如果您安装了Python,则可以尝试运行:

Instead of just opening the HTML file, try to host it using a web server. There are many options here. If you have Python installed, you could try running:

cd [directory where file is located]
python -m SimpleHTTPServer 8080

,然后在Web浏览器中的 http://localhost:8080

and then viewing it in a web browser at http://localhost:8080

我认为这可能是问题的原因是,在文件系统之外本地运行它时也会出错(尽管与您的错误不一样),但是当我通过Web浏览器查看它时,它工作正常,就像所以:

The reason I think this may be the issue is that I also get errors when running it locally off the filesystem (though not the same errors as you), but when I view it through a web browser, it works fine, like so:

https://jsfiddle.net/wwkdgL5b/

这篇关于无法使用Google代码在"DOMWindow"上执行"postMessage"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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