bob_cobb
bob_cobb

Reputation: 2269

Adding script src after window.onload

I'm trying to add this social widget after the body has loaded.

I first tried doing:

var stumble = '<script src="' + 'http://www.stumbleupon.com/hostedbadge.php?s=1' + '">       <\/script>';
$('#social-widget').append(stumble);

But that obviously didn't work, so I tried:

var stumble = document.createElement('script');
stumble.src = 'http://www.stumbleupon.com/hostedbadge.php?s=1';
$('#social-widget').append(stumble);

Which also didn't work. Any idea on what I am missing here?

Upvotes: 0

Views: 1330

Answers (3)

Chtioui Malek
Chtioui Malek

Reputation: 11515

the stumbleupon.com/hostedbadge.php?s=1 script only renders the button where it was placed and cannot work after document load.

instead use this script :

1/ place this tag where you want the button to appear

$('#social-widget').append('<su:badge layout="5"></su:badge>');

2/ load and execute the script :

jQuery.getScript('//platform.stumbleupon.com/1/widgets.js');

Upvotes: 0

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

Use the jQuery getScript method eg:

$.getScript('http://www.stumbleupon.com/hostedbadge.php?s=1');

Upvotes: 1

alessioalex
alessioalex

Reputation: 63663

You should really try something cross-browser, and since you're trying to load the script asynchronously, I strongly recommend Require.js.
You can use is standalone or alongside libraries like jQuery or Dojo.

Upvotes: 0

Related Questions