简体   繁体   中英

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?

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

Use append function provided by Jquery.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM