Skip to content

Instantly share code, notes, and snippets.

@jonathanconway
Created April 1, 2014 23:45
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonathanconway/9925286 to your computer and use it in GitHub Desktop.
Save jonathanconway/9925286 to your computer and use it in GitHub Desktop.
Reload (or initialize) addThis social share buttons. Useful when implementing addThis in a SPA (Single Page Application).
// Reload (or initialize) addThis social share buttons.
// IMPORTANT: make sure you put in a correct pubid on line 7.
window.addthis_reload = function () {
if (!window.addthis) {
// Load addThis, if it hasn't already been loaded.
window['addthis_config'] = { 'data_track_addressbar' : false };
$('body').append('<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid={YOUR PUBID HERE}"></script>');
} else {
// Already loaded? Then re-attach it to the newly rendered set of social icons.
// And reset share url/title, so they don't carry-over from the previous page.
window['addthis_share'].url = window.location.href;
window['addthis_share'].title = window.document.title;
window.addthis.toolbox('.addthis_toolbox');
}
}
@icelander
Copy link

Changing this:

window['addthis_config'] = { 'data_track_addressbar' : false };
$('body').append('<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid={YOUR PUBID HERE}"></script>');

to this:

window['addthis_config'] = { 'data_track_addressbar' : false, 'pubid': 'YOUR PUBID HERE };
$('body').append('<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js"></script>');

Will prevent problems with AddThis detecting the profile ID on the page.

@Jamiek94
Copy link

addthis.layers.refresh();

Did the trick for me.

@calpa
Copy link

calpa commented Nov 2, 2017

@Jamiek94 Thanks for the solution.

@internwt
Copy link

how i wil define addthis or import in react file?

@riyastir
Copy link

const addThisScript = document.createElement('script')
addThisScript.id = 'addthis-script'
addThisScript.src = 'https://s7.addthis.com/js/300/addthis_widget.js#pubid={YOUR PUBID HERE}'
document.body.appendChild(addThisScript)

Replace the Jquery append with Vanilla JS if you are not using Jquery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment