Tomas
Tomas

Reputation: 18117

Add JS tag before </body>

I need to place JS tag(<script src=...) as last tag before </body>. How to do that at runtime using JS or jQuery?

Upvotes: 0

Views: 2285

Answers (4)

dfsq
dfsq

Reputation: 193271

You could use Jquery .appendTo() :

Insert every element in the set of matched elements to the end of the target.

$('<script src="/path/to/script.js"></script>').appendTo('body');

Upvotes: 0

Lucky Murari
Lucky Murari

Reputation: 12782

Use append function provided by Jquery.

$(document).ready(function(){
 $('body').append('<script src="yourfile.js" type="text/javascript"> </script>');
});

Upvotes: 2

user497849
user497849

Reputation:

$( "body" ).append(
  "<script src=\"your script source\" type=\"text/javascript\"></script>"
);

and if you want to add it as the first element inside body use prepend

$( "body" ).prepend(
  "<script src=\"your script source\" type=\"text/javascript\"></script>"
);

Upvotes: 1

nico
nico

Reputation: 51650

var myscript = $("<script src='...'>...</script>");
$("body").append(myscript);

Upvotes: 2

Related Questions