Load external Javascript on function call

Ronal picture Ronal · Sep 3, 2009 · Viewed 17k times · Source

I would like to know how to load an external Javascript into my document from a function.

Answer

seth picture seth · Sep 3, 2009

This is one way:

function loadDaFun() {
   var script = document.createElement('script');
   script.src = '/path/to/your/script.js';
   var head = document.getElementsByTagName("head")[0];
   head.appendChild(script);
}