【问题标题】:Google Plus One Button - How to add a callback?Google Plus One Button - 如何添加回调?
【发布时间】:2012-06-21 20:40:35
【问题描述】:

我的网站上呈现的 google+ 按钮如下:

在JS中:

    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);

在html中:

<g:plusone size="medium" href="https://site.com"></g:plusone>

如何添加回调来检测用户何时点击?我在这里看到文档:https://developers.google.com/+/plugins/+1button/#jsapi 没有看到如何使用当前渲染实现。谢谢

【问题讨论】:

  • 你注意到参数对象的“回调”属性了吗?我想这就是你想要的。
  • @Pointy 会在哪里添加?
  • 你在标签里设置:&lt;g:plusone callback='functionName' ...&gt;
  • 谢谢,试过了,但始终没有生效... site.com" callback="alert('gotime')">
  • 我认为回调参数应该只是一个全局函数的name。但是,很难从该文档中分辨出来。

标签: javascript jquery api google-plus


【解决方案1】:

您可以使用回调属性向您的 +1 按钮标记添加 JavaScript 回调。提供驻留在全局命名空间中的函数的名称。当用户点击 +1 按钮时,它将被调用。

您的代码可能如下所示:

<script>

   function myCallback(jsonParam) {

      alert("URL: " + jsonParam.href + " state: " + jsonParam.state);

   }

</script>

<g:plusone size="medium" href="https://site.com" callback="myCallback"></g:plusone>

您可以在tag parameters section of the documentation 中了解有关此属性的更多信息。

【讨论】:

  • 出于好奇,回调是如何工作的?即父窗口中的javascript如何获取访问子窗口(Google Oauth)的状态?
  • 在此处发布上述问题:stackoverflow.com/questions/27934815/…
【解决方案2】:

除了 Jenny 的回答之外,您还可以使用 HTML5-valid +1 标记,方法是将 class 属性设置为 g-plusone,并在任何按钮属性前加上数据 - 例如回调

<div class="g-plusone" data-callback="myCallback" ></div>

来自https://developers.google.com/+/web/+1button/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-25
    • 2011-12-23
    • 1970-01-01
    • 2015-03-12
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多