English 中文(简体)
Issue Loading Google +1 Widget Asynchronously
原标题:

I have a few widgets on the site I m developing and I load them all asynchronously from a javascript file so it does not hold up the DOM from finishing.

For instance, I do this with the Digg and Buzz widgets and it works fine:

// Buzz Share
function buzzShare() {
    $jQ( .sharebox ).append( <div class="widget"><a title="Post to Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="normal-count"></a></div> );
    $jQ.getScript( http://www.google.com/buzz/api/button.js );
}
// Digg Share
function diggShare() {
    $jQ( .sharebox ).append( <div class="widget"><a class="DiggThisButton DiggMedium"></a></div> );
    $jQ.getScript( http://widgets.digg.com/buttons.js );
}

When it comes to the new Google +1 widget, the same logic does not work:

// PlusOne Share
function plusOneShare() {
    $jQ.getScript( http://apis.google.com/js/plusone.js );
    $jQ( .sharebox ).append( <div class="widget"><div class="g-plusone" data-size="tall" data-count="true"></div></div> );
}

I tried using both the HTML5 tag and <g:plusone></g:plusone>. Neither work.

Here is the documentation for the just-launched service: http://code.google.com/apis/+1button/

I also noticed you can do the following if embedding the script directly into the HTML.

 <script type="text/javascript" src="https://apis.google.com/js/plusone.js">
      {"parsetags": "explicit"}
    </script>

Is there a way to use the {"parsetags": "explicit"} parameters with jQuery .getScript?

P.S. I also tried switching around the first and second lines within the plusOneShare function, that didn t work either.

Thanks!

最佳回答

The method I described in my question works perfectly. There was merely some sort of bug on Google s end if I am correct. I was trying to get the feature working just a few hours after they announced it. About two days later, it now works perfectly.

The .getScript method is a good way to load the +1 via AJAX.

问题回答

What browser are you using? The following full page example works for me:

<html>
  <head>
    <title>jQuery Dynamic load test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $.getScript( https://apis.google.com/js/plusone.js );
        $( .sharebox ).append( <div class="widget"><div class="g-plusone" data-size="tall" data-count="true"></div></div> );
      });
    </script>
  </head>
  <body>
    Hello world!
    <div class="sharebox"></div>
  </body>
</html>

I ended up doing something like this to support parameter passing to the +1 script:

var hd = document.getElementsByTagName( head )[0];
var scr = document.createElement( script );
scr.type = text/javascript ;
scr.async = true;
scr.innerHTML = "{lang:  es-419 }"; // Magic! :)
scr.src = "https://apis.google.com/js/plusone.js";
hd.appendChild(scr);

As showed on http://code.google.com/intl/pt-BR/apis/+1button/#plusonetag-parameters

you can do an explicid load using

$(document).ready(function() { gapi.plusone.go( content ); });

Another solution for async loading. Works perfectly for me, especially if you re using php and want to dynamically assign the language.

<script type="text/javascript">
    window.___gcfg = {
<? if ($lang!=  en ){    ?>
     lang:  <?=$lang?> , 
<? } ?>
     parsetags:  explicit ,
     annotation:  inline ,
     size:  medium 
    };
    $(document).ready(function() { 
        $.getScript( https://apis.google.com/js/plusone.js ,function(){
            gapi.plusone.render("plusone-div");
        });
    });
</script>

If you use getScript (or any asynchronous loading method), you end up with a blank page on unsupported browsers. plusone.js replaces your whole page on iphone/ipad/android etc..

I m also looking for a solution to be able to use parsetags: "explicit" with getScript..

Now, I also did some research on this - and I LOVE $.getScript - (not in a creepy way, but only because it speeds up the initial display of my all pages). If you ve ever built a website that contains references to Twitter, Facebook, Plusone, OpenX, etc, you know what I m talking about :)

Anyway, what I recently figured out is that "plusone.js" can be loaded async (using jQuery $.getScript) and at the same time be instructed to use a certain language:

<script type="text/javascript">
  window.___gcfg = { lang:  sv-SE  };
  $(document).ready(function() { 
     $.getScript("https://apis.google.com/js/plusone.js");
  });
</script>

<body>
   <g:plusone size="medium"></g:plusone>
</body>

Hope this helps anyone :)





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签