Change AddThis url dynamically with jQuery
Asked Answered
L

7

17

I am trying to dynamically change the URL sent by addthis. When a user alters an element it updates a text area containing a custom url so they can return to that url and continue/view their work.

I am creating an addthis button like so(from their API docs):

var addthis_share = {url:"http://www.johndoe.com"}
$(document).ready(function(){
    var tbx = document.getElementById("toolbox"),
    svcs = {email: 'Email', print: 'Print', facebook: 'Facebook', expanded: 'More'};
    for (var s in svcs) {
        tbx.innerHTML += '<a class="addthis_button_'+s+'">'+svcs[s]+'</a>';
    }
    addthis.toolbox("#toolbox");
});

Then when the url is updated I am trying to update the addthis share URL like so:

function updateURL(){
    ...get some variables here and generate a new url
    var newURL="http://the.url.i.want.to.share.com";
    $('#tagUrl').val(newURL);
    //addthis_share = {url:newURL}
    addthis_share = {url:newURL}
    addthis.toolbox("#toolbox");
} 

The original buttons are getting generated and contain the correct url, but when the url update function executes the addthis share url is not getting updated. How can I force it to update the addthis url?

Lomalomas answered 12/3, 2013 at 16:44 Comment(4)
You want to reload the page ? window.location.reload()Rudder
No...this url changes very frequently.Lomalomas
Hmm it's weird because your var addthis_share looks like to be global. You should try reloading the AddThis plugin or see if you can change the url in the DOM directly.Rudder
Thank you for your help: addthis.update('share', 'url', newURL); solved the problem.Lomalomas
A
26

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

Avens answered 9/4, 2013 at 6:20 Comment(2)
hi can we change the discription in this waySeale
Set "addthis:url" and "addthis:title" on each sharing toolbox you have on your page and call: addthis.toolbox(".addthis_toolbox"); Don't change the selector. See my example at jsfiddle.net/bs7g9rm8Varicolored
U
17

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

Useless answered 17/1, 2014 at 1:50 Comment(0)
C
1

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>
Cristinacristine answered 25/5, 2017 at 11:20 Comment(0)
R
0

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";
}
Rubbico answered 3/5, 2013 at 18:42 Comment(0)
K
0
<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.

Kindness answered 3/11, 2013 at 16:29 Comment(1)
Where does isSameNode come from?Cementum
V
0

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;

Verla answered 24/8, 2015 at 15:30 Comment(0)
V
0

Just works for now

window.addthis.layers.refresh('new_url', 'new_title');

Another configs should be absent (like dom attributes) should be absent

Vitascope answered 4/9, 2020 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.