如何在iframe中加载谷歌加分享按钮

How to load google plus share button in iframe?

本文关键字:谷歌 分享 按钮 加载 iframe      更新时间:2023-09-26

谁能给我一个解决方案,关于如何在 iframe 中异步加载 Google plus 共享按钮(不是 +1 按钮(。我已经设法为 +1 按钮做到了这一点。

试试这个 html 片段:

<iframe src="https://plusone.google.com/_/+1/fastbutton?bsv&amp;size=medium&amp;hl=en-US&amp;url=http://test.com/&amp;parent=http://test.com/" allowtransparency="true" frameborder="0" scrolling="no" title="+1"></iframe>

如果您尝试做的是在用户将鼠标悬停在某物上时显式呈现按钮,则需要做的是使用显式呈现流,如下所述:

https://developers.google.com/+/plugins/+1button/

但是,有一个转折点,因为您使用的是共享按钮,因此无法使用显式呈现代码直接呈现到div。相反,您将创建共享链接,将呈现设置为显式,然后可以使用 JavaScript 触发共享按钮的呈现。

相关代码为:

<html>
  <head>
    <title>+Share: Explicit render</title>
    <link rel="canonical" href="http://www.example.com" />
    <script type="text/javascript">
    window.___gcfg = {
      lang: 'en-US',
      parsetags: 'explicit'
    };
    function renderShare() {
      gapi.plus.go(); // Render all the buttons
    }
    </script>
  </head>
  <body>
    <a href="#" onClick="renderShare();">Render the share control</a>
    <!-- a share button that will get rendered when gapi.plus.go is called -->
    <div class="g-plus" data-action="share" id="share-div"></div>
  </body>
  <script type="text/javascript">
    // Asynchronously load the plusone script, for performance
    (function() {
      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);
    })();
  </script>
</html>

对于您来说,我猜您正在尝试使用不同的触发器来呈现 +1 按钮,而不是用户明确必须单击的按钮,您可以只使用适当的事件来触发渲染。