0

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?

2
  • possible duplicate of add javascript programmatically Commented Nov 18, 2011 at 21:19
  • @Diodeus Tried that method (breaking up the script tags so the parser won't recognize them), but it didn't work. Commented Nov 18, 2011 at 21:26

3 Answers 3

1

Use the jQuery getScript method eg:

$.getScript('http://www.stumbleupon.com/hostedbadge.php?s=1');
Sign up to request clarification or add additional context in comments.

2 Comments

How would this insert the the button into the DOM after the page has loaded?
It wouldn't necessarily insert the script in the DOM, it would however load and execute the script, which I presumed is what you're after? Why else would you want to append a script tag?
0

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');

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.