4

I have code:

let script = document.createElement("script");
script.src = "https://example.com/script.js";
document.head.append(script);
loadStuff();

How do I run loadStuff after loading script?

4
  • script elements have an onload event. You can hook the function call there: testScript.onload = someFunctionFromScript;. Commented Oct 26, 2018 at 14:25
  • @Shilly not working: Uncaught ReferenceError: someFunctionFromScript is not defined Commented Oct 26, 2018 at 14:31
  • 2
    then try testScript.onload = function() { someFunctionFromScript(); }. Commented Oct 26, 2018 at 14:37
  • Oh, it works now. Thank you Commented Oct 26, 2018 at 14:38

1 Answer 1

2

Answering my own very old question:

let script = document.createElement("script");
script.src = "https://example.com/script.js";
script.onload = loadStuff;
document.head.append(script);
Sign up to request clarification or add additional context in comments.

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.