2016-04-26 39 views
0

我想在我的網站上添加Google+徽章。當我從Google配置工具複製/粘貼代碼時,徽章不起作用。我想這個問題可能是在JavaScript,所以我創建了一個空的index.html和複製/粘貼谷歌代碼...但它仍然不能正常工作:無法使用Google代碼在'DOMWindow'上執行'postMessage'

谷歌代碼:

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

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

在控制檯中,我可以看到以下錯誤:

  • 無法執行 '的postMessage' 上' DOMWindow':提供的目標原點('null')與收件人窗口的原點('null')不匹配。人
  • 不贊成使用getAttributeNode()。改用getAttribute()。 '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
+0

[適用於我。](https://jsfiddle.net/b9x4svf9/)你想把它放在iframe或其他東西? –

+0

嗯......即時通訊使用完全相同的HTML代碼,就像我在這裏發佈的......並不起作用。我曾嘗試Firefox,Firefox Developer和Chrome。不能正常工作:-( – steelbull

+0

[Javascript中的Google API]的可能重複(http://stackoverflow.com/questions/21710842/google-api-in-javascript) –

回答

2

問題可能在於,Google+徽章可能要求您通過網絡服務器運行代碼,而不是通過文件系統在本地查看代碼。

而不是隻打開HTML文件,嘗試使用Web服務器託管它。這裏有很多選擇。如果你已經安裝了Python,你可以嘗試運行:

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

,然後在http://localhost:8080


在Web瀏覽器中查看它

我覺得這可能是問題的原因是,我也得到錯誤本地運行它時,關閉文件系統(雖然不是同一個錯誤,你的),但是當我通過Web瀏覽器查看它,它工作正常,像這樣:

https://jsfiddle.net/wwkdgL5b/

+0

Yeeees!這是可靠的答案。它需要運行thrue網絡服務器,謝謝。 – steelbull

相關問題