AddThis buttons wont update to include fragment (#Hash Tag)

前端 未结 1 1216
我寻月下人不归
我寻月下人不归 2020-12-17 06:29

I have these AddThis buttons on my website, and my site uses JS to take a user though an image gallery, when changing an image the URL is updated with the image title in the

相关标签:
1条回答
  • 2020-12-17 06:54

    I had a similar issue whilst using hashbangs for a javascript driven site. This code was used because the share buttons wouldn't re-render when content is loaded async. You should be able to take parts of this to fit to your situation. Maybe just the addthis.update() code

    First I changed the script for addThis to async

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

    Once the DOM has loaded, listen for hashchanges, this is where we will update the addthis share details.

    You may require additional plugins for this bit to work with older browsers, consider jQuery hashchange event - v1.3 - 7/21/2010 - http://benalman.com/projects/jquery-hashchange-plugin/

    Now its all wired up for a hashchange, the next step is to alter 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.
        });
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题