In a SPFx solution, I'm importing a JavaScript library like this:

import './javascriptfile.js';

This JS file gets executed before the DOM is ready. If we are using a SCRIPT tag, we can use defer attribute that executes JS file after completing the DOM parsing.

Is there an equivalent method in TypeScript which executes the JS file after completing the DOM parsing?

  • 1
    Check this article. let me know if it helps! Nov 12, 2021 at 8:49
  • @Suresh, You are waiting for DOM to be ready. But what exactly are you waiting for? You want to wait for some out-of-the-box SharePoint Online component to finish loading? What is this component? Nov 13, 2021 at 21:49
  • @Ganesh Sanap, I followed the steps mentioned in that article and it worked! Basically I'm loading the scripts after rendering the HTML. If you can post this as an answer I can mark it as resolved. Thank you.
    – Suresh C
    Nov 15, 2021 at 10:11
  • @Denis, I have referred Angular javascript files. These files need to be executed only after the DOM is parsed because in the DOM i'm referring to the angular element.
    – Suresh C
    Nov 15, 2021 at 10:13
  • @SureshC Great, glad it worked for you. I have posted this as an answer below. Nov 15, 2021 at 10:19

1 Answer 1


As you are directly importing the JavaScript file, it gets executed before the DOM is ready.

Instead of this approach, you can load the scripts after rendering the HTML/DOM.

Follow this article for more information: Declaratively loading JavaScript

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.