2

I've included a script in jQuery, but it doesn't seem to work. Can you take a look at the code and enlighten me?

<div id="foo"></div>

$('#foo').append('<script> some code </script>');
1
  • Please format this code better. Putting each statement onto separate lines will show a good faith effort on your part and make it easier for others to read your code and answer your question. Commented Dec 16, 2011 at 17:54

2 Answers 2

3

I would strip your script out off the page into its own nice clean file and then pull it in via jquery

    var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = url;
$("#someElement").append( script );
Sign up to request clarification or add additional context in comments.

2 Comments

@Dominic Green how can we pass variable within the append tag for eg in the above example: $("#someElement").append(script(q)); can we do it??
Its not a function so you cant pass in a variable. However if you declare a variable above the append your code should be able to access it
2

Seeing as you are using javascript to insert some javascript, why not just run the javascript snippet:

!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

Where you have:

$('#foo').append(...);

Just insert that code from Twitter. It will do the same thing.

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.