0

In the application I'm developing I need to include some javascript files dynamically after the page is loaded. How to acheive this?

2 Answers 2

5

You can load a script resource dynamically using the following function

function loadScript(src){
    var script = document.createElement("script");
    script.src = src;
    document.body.appendChild(script);
}

Here we are creating a script element dynamically and add that to the DOM structure of the page.

If you are using jQuery, you can use the getScript() method to fetch a script resource dynamically.

Sign up to request clarification or add additional context in comments.

Comments

0

Add dynamic script resource to a page

function loadScript(src){
var script = document.createElement("script");
script.src = src;
document.body.appendChild(script);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.