0

How can I add script tags inside a div using jquery .

I want it like

<div id="scriptContainer" >
       <script type="text/javascript" src="script.js"></script>
      <script type="text/javascript" src="script2.js"></script>
</div>

I used it with jquery appendTo but it does not work.Could you please help me

4
  • script add in head not in div Commented Nov 27, 2013 at 6:27
  • Post your jquery code!!! Commented Nov 27, 2013 at 6:29
  • What's the reason for adding a script to a DIV and not in your header or before your closing <body /> tag? Commented Nov 27, 2013 at 6:32
  • If you're trying to document.write() content into that div from these scripts, you cannot do it after the document has been loaded like this. The scripts will have to be modified to append content to that div. Commented Nov 27, 2013 at 6:35

2 Answers 2

1

Try using jQuery.getScript():-

$.getScript("script.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
});

Reference

Sign up to request clarification or add additional context in comments.

1 Comment

@Johnsmith Does this answer your query>
0

You can create it as an object and then append it.

var script = document.createElement( "script" );
script.type = "text/javascript";
script.src = "script.js";
$("#scriptContainer").append(script);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.