Change AddThis url dynamically with jQuery

China☆狼群 提交于 2019-12-03 05:55:49
sunnyuff

If you wants to change the addthis url after the html is loaded (in case of ajax or some user events),  please use the below hack . Addthis apis are broken as on [09/04/13 11:42:24 AM] sunny jhunjhunwala:

addthis.update('share', 'url', 'http://www.sourcen.com'); 
addthis.url = "http://www.sourcen.com";                
addthis.toolbox(".addthis_toolbox");

http://www.sourcen.com ---> new url

It is as simple as this:

addthis.update('share', 'url', "http://www.example.com");
addthis.update('share', 'title', "Example Domain Title");
addthis.update('share', 'description', "Hello, I am a description");

Cheers, Gary

AddThis provides addthis.update("share", "url", value) function (but it is a buggy function in IE8); try this one:

for(var i = 0; i < addthis.links.length; i++){
    addthis.links[i].share.url= "new url";
}
<span id="share-obj" addthis:url="" addthis:title=""></span>

<script type="text/javascript">
    var shareConfig = {url: '', title: ''};

    addthis.button('#share-obj', {}, shareConfig);

    addthis.addEventListener('addthis.menu.open', function(event){
        if ( ! event.data.element.isSameNode($('#share-obj')[0])) {
            return;
        }

        event.data.share.title = shareConfig.title;
        event.data.share.url = shareConfig.url;
    });

    // in updateURL() or any other place during runtime...
    function updateURL() {
        shareConfig.url = 'http://different.url';
        shareConfig.title = 'Title changed dynamically...';
    }
</script>

I used a global variable shareConfig to keep track of the URL and title configuration. I have multiple addthis buttons in a webpage, each sharing different content. Therefore I have added a isSameNode to make sure I only update the button I want.

Although URL and title are changed dynamically, the addthis:url and addthis:title attributes must be present in the button (<span>), or else it won't work.

Step-1: Preparing ajax content to be loaded with other HTML;

<div class="addthis_sharing_toolbox-CUSTOM" data-url="<?php my_dynamic_link(); ?>" data-title="<?php my_dynamic_title(); ?>">
    <a class="addthis_button_email" style="cursor:pointer"></a>
    <a class="addthis_button_facebook" style="cursor:pointer"></a>
    <a class="addthis_button_twitter" style="cursor:pointer"></a>
    <a class="addthis_button_linkedin" style="cursor:pointer"></a>
</div>

Step-2: Preparing my fancybox ajax setup;

$('.popUpBtn').fancybox({
    //....other fancybox settings can go here...
    //....
    afterShow   : function(current, previous){
        //addthis.init();
        addthis.update('share', 'url', $('.fancybox-inner .addthis_sharing_toolbox-CUSTOM').data('url'));
        addthis.update('share', 'title', $('.fancybox-inner .addthis_sharing_toolbox-CUSTOM').data('title'));
        //addthis.update('share', 'description', "Hello, I am a description");
        addthis.toolbox('.addthis_sharing_toolbox-CUSTOM'); //-->Important: this selector should not be anything from the addthis settings page; We can choose any other names like normal selectors;
    }
});

The above lines inside the "afterShow()" function are important to keep a note;

To customized the button:

<a class="addthis_button_compact" >
   <img src="./images/share.png" width="36" height="36" border="0" alt="Share" />
</a>

Head Part:

<meta property="og:title" content="YOUR TITLE" />
<meta property="og:description" content="YOUR DESCRIPTION" />
<meta property="og:image" content="YOUR IMAGE URL" />

Body Part: In my case I am using php I set the body attributes like this.

<body
      data-url="<?php echo basename($_SERVER['REQUEST_URI']); ?>"
      data-title="<?php echo $info->record_info->brand; ?>"
      data-description="<?php echo $info->record_info->description; ?>"
      data-media="<?php echo ./uploads/logos/<?php echo $info->record_info->logo; ?>"
>

To update content dynamically (JS Part):

<script>
    addthis.update('share', 'url', $('body').data('url')); // will set the URL
    addthis.update('share', 'title', $('body').data('title')); // will set the title
    addthis.update('share', 'description', $('body').data('description')); // will set the description
    addthis.update('share', 'media', $('body').data('media')); // will set the image if you are sharing
</script>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!