点击加载 Google 加 javascript 显示评论

click to load Google plus javascript to show comments

您好,我想加载来自 google plus 的评论。 post 哪些人在 google 上以及来自我的网站。 我实际上只是在 WordPress 博客 post 中安装了这段代码。如果用户想通过单击 Google+ 按钮从 Google 加载代码以及加载评论来回复 post。

<button onClick="showGoogle();">Google+</button>

<div style="max-width:100%" id="loading">
<div id="gpcomments" style="max-width:100%"></div>
</div>

<script>
gapi.comments.render('gpcomments', {
    href:'http://findsgood.com/?p=43224',
    width: '682',
    first_party_property:'BLOGGER',
    view_type: 'FILTERED_POSTMOD','callback' : function showGoogle() {src='https://apis.google.com/js/plusone.js';}});
</script>

终于找到了有效的代码,但我必须点击按钮两次。我不知道为什么我需要点击两次?请有人帮忙?

<button onClick="showGoogle();">Google+</button>

<div style="max-width:100%" id="loading"><div id="gpcomments" style="max-width:100%"></div></div>

<script>function showGoogle() {
    var head= document.getElementsByTagName('head')[0];
      var script= document.createElement('script');
      script.type= 'text/javascript';
      script.src= 'https://apis.google.com/js/plusone.js';
      head.appendChild(script);
gapi.comments.render('gpcomments', {
    href:'http://findsgood.com/?p=43224',
    width: '682',
    first_party_property:'BLOGGER',
    view_type: 'FILTERED_POSTMOD'
});}
</script>

你可以运行在博客上测试posthttp://findsgood.com/?p=43224

第一次点击会在控制台中生成错误,为了测试,请尝试手动将 plusone.js 包含在主题 header 中。 (这感觉更像是评论,只是还不能添加)。

plusone.js 正在异步加载,但 gapi 正在同步调用。在 plusone.js 完成加载之前,gapi 不可用。您可以使用 onload 事件等待 gapi 准备好使用。

<button onClick="showGoogle();">Google+</button>

<div style="max-width:100%" id="loading"><div id="gpcomments" style="max-width:100%"></div></div>

<script>
function showGoogle() {
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://apis.google.com/js/plusone.js';
    script.onload = function() {
        gapi.comments.render('gpcomments', {
            href:'http://findsgood.com/?p=43224',
            width: '682',
            first_party_property:'BLOGGER',
            view_type: 'FILTERED_POSTMOD'
        });
    }
    head.appendChild(script);
}
</script>