javascript - 单击加载 Google plus javascript 以显示评论

标签 javascript wordpress html google-plus blogger

嗨,我想从 google plus 加载评论。人们通过我的网站在 google plus 上发布了哪些内容。 我实际上刚刚在 WordPress 博客文章中安装了这段代码。如果用户想要通过单击 Google+ 按钮来从 Google+ 加载代码并加载评论来回复帖子。

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

最佳答案

plusone.js 正在异步加载,但 gapi 正在同步调用。 gapiplusone.js 加载完成之前不可用。您可以使用 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>

关于javascript - 单击加载 Google plus javascript 以显示评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47484050/

相关文章:

html - 创建 Web 应用程序,然后向其中添加 Ajax?

javascript - dataLayer.push() 返回值含义

javascript - 具有固定侧边栏布局的 NuxtJs nuxt-link 滚动行为

css - 如何编辑特定页面的 html/css?

javascript - 如何通过 API 调用附加到 WordPress 定制器中的选择框?

html - Youtube 的 HTML5 视频播放器如何控制缓冲?

javascript - Node.js 的 http.Server 和 http.createServer,有什么区别?

javascript - 简单的Backbone.js集合无限分页

php - 与用户关联的 ACF update_field

javascript - 拆分一个 HTML 页面以在多个屏幕上显示不同的 View