How to load javascript code to an html file at runtime?

Paste this within onclick of that button (correct the url in 3rd line):

var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "url to the script file here");
document.getElementsByTagName("head")[0].appendChild(script);

That script will start being downloaded immediately after line 4 is executed, and as soon as it’s downloaded it’ll execute or otherwise be usable.

Leave a Comment