AddThis按钮不会更新为包含片段(#Hash Tag)

时间:2011-09-22 12:42:14

标签: javascript facebook facebook-like addthis

我的网站上有这些AddThis按钮,我的网站使用JS通过图片库来吸引用户,更改图片时,使用片段元素中的图片标题更新URL,我遇到的问题是这是没有反映在AddThis链接中。我试图从AddThis论坛得到一些答案,但是指出了api文档和addthis.toolbox()函数,但它似乎并不适合我,所以我想我会运行它过去这里的专家。

这是我在基本页面上的AddThis代码。

<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
<a class="addthis_button_preferred_1 add_this_but"></a>
<a class="addthis_button_preferred_2 add_this_but"></a>
<a class="addthis_button_preferred_3 add_this_but"></a>
<a class="addthis_button_preferred_4 add_this_but"></a>
<a class="addthis_button_compact add_this_but"></a>
<a class="addthis_counter addthis_bubble_style"></a>
</div>
<script type="text/javascript">var addthis_config = {"data_track_clickback":false};</script>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4e77111e2de880eb"></script>

<!-- AddThis Button END -->

在我的JS脚本中,当用户更改图像时,我已将其添加到函数中。

addthis.toolbox(".addthis_toolbox");

我也试过这个

addthis.toolbox();

运行代码时,我没有JS错误,但我没有看到AddThis按钮的任何更新。

你可以在这里看到问题。

http://www.martynazoltaszek.com/artpages?nid=30#Rendezvouz

此链接将在名为“Rendezvouz”的特定图像上打开网站,但是nid = 30实际上是一个名为“Walking by”的不同图像。问题由AddThis(Facebook)显示将始终链接到Walking by “图片。添加此Gmail似乎没问题,但我不认为这是API的一个因素,因为它可以使用或不使用上述函数调用。

提前致谢。

P.S。使用?gid GET变量和Fragment的原因是由于php页面加载和JS查看(没有page referh),但这也支持非JS浏览器。

1 个答案:

答案 0 :(得分:12)

我在使用hashbangs为javascript驱动的网站时遇到了类似的问题。使用此代码是因为当内容加载异步时,共享按钮不会重新呈现。你应该能够采取一些适合你的情况。也许只是addthis.update()代码

首先,我将addThis的脚本更改为async

http://s7.addthis.com/js/250/addthis_widget.js#async=1

加载DOM后,请监听散列更改,这是我们更新addthis共享详细信息的地方。

此位可能需要额外的插件才能与旧版浏览器一起使用,请考虑jQuery hashchange事件 - v1.3 - 7/21/2010 - http://benalman.com/projects/jquery-hashchange-plugin/

现在它全部用于哈希交换,下一步是改变addThis。

    $(function(){
        $(window).hashchange(function(){
            if($('body').hasClass('addThis')){
                addthis.ost = 0;
                addthis.update('share', 'url', window.location.href); // new url
                addthis.update('share', 'title', window.document.title); // new title.
                addthis.ready(); // this will re-render the buttons.
            }
            else {
                addthis.init();
                $('body').addClass('addThis');
            }
        });
        addthis.init() //As we've changed the addthis to async, you will have to call addthis.init() once the DOM has loaded.
    });

希望这有帮助。