Reputation: 18117
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
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
Reputation: 12782
Use append function provided by Jquery.
$(document).ready(function(){
$('body').append('<script src="yourfile.js" type="text/javascript"> </script>');
});
Upvotes: 2
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
Reputation: 51650
var myscript = $("<script src='...'>...</script>");
$("body").append(myscript);
Upvotes: 2