1

I have a link that a user can share on fb and G+.

The user can modify that link and he did it with an ajax call so the page is not refreshing.

My gplus button code :

<link rel="canonical" href="{{ url }}" />
<g:plus action="share" style="height: 28px;" ></g:plus>

when the user changes the url I update the href attribute too :

$.post(url,{
       someParams:"someValues"
   },function(data){
 .
 .
 .
 // updating the href attribute with the new modified url 
 $('link[rel="canonical"]').attr("href",data.url);
 }

The problem is that the G+ button still load the old url. But only by refreshing I get the new one.

My question is how can i change the G+ url without refreshing the page ?

4

1 回答 1

1

这是你想要的(显然改变了你的 ajax 回调逻辑的提示)?

http://jsfiddle.net/coma/2783J/6/

HTML

<!-- Place this tag where you want the +1 button to render. -->
<div id="gplus"></div>
<button>change it!</button>

JS

$(function() {

    var container = $('#gplus');
    var render = function(url) {

        container.html('<g:plusone href="' + url + '"></g:plusone>');
        gapi.plusone.go('gplus');

    };

    $('button').click(function(event){

        var url = prompt('Give me an URL');
        render(url);

    });

    render(location.href);

});

请注意,我正在将 plusone.js 加载到该 jsfiddle 的外部资源中。

顺便说一句,你为什么使用 Symfony2 标签?

于 2013-05-28T18:46:45.910 回答