Tutorial: Load the Google +1 button asynchronously


In our previous article, we explained how to produce a valid Google +1 button. That’s nice, but you will probably notice that the button is rendered only after your website is completely loaded. This approach is problematic, especially regarding the fact that during the loading of the script, your page appears to be frozen. And let’s say it, this button is slow!

Fortunately, there are some solutions to circumvent this problem. It is possible to load scripts asynchronously so that it does not block your webpage rendering, and the script will be ready to use as soon as it is loaded.

We will use a technique similar to the one used by Google Analytics, with the difference that we had to modify it a little so that it is still possible to customize the button while speeding it up a little.

Ready? Let’s go !


First add your DIV tag:

<div id="plusone-div"></div>

Nothing complicated. Note that you must not add the “g-plusone” class to this DIV.

Then anywhere after the DIV tag, add the following code:

<script type="text/javascript">

function getElementByIdUniversal( id )
	return ( document.getElementById ) ? document.getElementById( id ) : document.all[ id ];
function plusoneready()
	gapi.plusone.render( "plusone-div", { "size": "tall", "count": "true" } );

(function() {
	var gp   = document.createElement( "script" );
	gp.type  = "text/javascript";
	gp.async = true;
	gp.src   = "https://apis.google.com/js/plusone.js";

	gp.onload = plusoneready;
	// Only for IE 6 and 7
	gp.onreadystatechange = function()
		if( this.readyState == "complete" )
	var div = getElementByIdUniversal( "plusone-div" );
	div.parentNode.insertBefore( gp, div );




Let’s explain this code a little so that you can understand it faster:

  1. The function “getElementByIdUniversal()” returns the element corresponding to the specified “id” parameter. It is similar to the “getElementById” function but it also work with old versions of Internet Explorer.
  2. The function “plusoneready()” is a callback that will be called once the Google +1 Javascript file is loaded.
  3. Then we begin a self-running anonymous function.
  4. We create a SCRIPT element of type ‘text/javascript’.
  5. We take care to set the “async” member to “true” so that the script will load asynchronously.
  6. We set the “src” member to “https://apis.google.com/js/plusone.js”. Note that you should use “https” and not “http” if you want to avoid the cost of a useless redirection…
  7. We set the “onload” member to be our callback function.
  8. For IE 6 and 7, we have to use the “onreadystatechange” member to be able to call our callback.
  9. We find our DIV element using its ID
  10. We add our SCRIPT tag just before our DIV
  11. End of the anonymous function



And that’s it! Your Google +1 button will now load asynchronously and should not stop other elements from rendering.

If you have some suggestions to improve the previous code or found any issue, do not hesitate to give your feedback in the comments!

Happy coding.

View Comments (11)

  • Much thanks but I am getting a Javascript Error in latest version of Chrome. Sometimes the button loads fine despite the error but sometimes it doesn't load at all.

    The error is: UncaughtTypeError: Cannot read property 'parentnode' of null.

    Right after the last line of the code.

    Any ideas?


  • Hi Joe,

    The only explanation is that when the Javascript code is executing, the DIV with ID of "plusone-div" was not found, thus you have this error. Be sure to place this Javascript section after the "plusone-div" DIV element so that it always find it.



  • If Gmail is not login the pop-up window will display while clicking G+ (google Plus) button. how restrict the pop-up window? and how to get gmail account login status? Please help me. Thanks in advance.

  • Please gabriel i have done this process, but i am having issues when i re-analyse my site using the google page analysis tool, it still shows that the plusone.js script is still a problem

    • Hi Moussya,

      Google is shutting down its Google+ network, so it's simply not worth spending any energy to fix what might not work :-)