Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  65] [ 4]  / answers: 1 / hits: 24262  / 13 Years ago, fri, january 27, 2012, 12:00:00

Let me ask whether it is possible to load javascript code to an html file at runtime. For example , place a textbox to input the location of the script files and a form button to trigger loading the script files.



Thank you in advance.


More From » javascript

 Answers
18

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.


[#87772] Wednesday, January 25, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
willieelisham

Total Points: 201
Total Questions: 108
Total Answers: 106

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;