Load JavaScript dynamically

Load JavaScript dynamically

This is the snippet you can use.

function loadScript() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://example.com/some.js";
    document.body.appendChild(script);
}

The code is self explained. You have to add only onload event in the body tag or this at the bottom of the page

window.onload = loadScript();
Tagged with: , ,
Posted in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Sites
Categories
Archives