Michael Samuel
Michael Samuel

Reputation: 3920

Custom Google plus button callback

I have this simple HTML layout:

<html>
   <head>
   <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

   <script>
   gapi.plusone.render("mybutton");

   function myCallback(jsonParam) {
       alert("URL: " + jsonParam.href + " state: " + jsonParam.state);
   }
   </script>
   </head>

   <body>
      <a target="_blank" id="mybutton" data-callback="myCallback" href="https://plus.google.com/share?url=http%3A%2F%2Fgoogle.com">Share on G+</a>
   </body>

</html>

I have a custom google plus button with id called mybutton. I render it as a plus button using gapi.plus.render function.

I have a callback function that should render when a URL is shared on G plus but for some reason the event is not firing. Is there something wrong in my code or the callback only works with native gplus buttons?

Upvotes: 1

Views: 1710

Answers (1)

3abqari
3abqari

Reputation: 208

You'll need to put the script tag at the end of the page (right after the body tags), so that the page rendering would finish before you call your script.

Correction:

Use the following code inside the script tags to render that button with the callback in the rendering call:

gapi.plusone.render("mybutton", { "callback": myCallback });

Upvotes: 1

Related Questions