How to force a script to reload and re-execute with JavaScript?

Spread the love

To force a script to reload and re-execute with JavaScript, we append a new script tag.

For instance, we write

const head = document.head;
const script = document.createElement("script");
script.src = "source_file.js";
head.appendChild(script);

to create a new script element with createElement.

Then we set its src property to the script’s URL.

Thewn we call head.appendChild to append the script element as the last chold of the head element to load and run it.

Leave a Reply

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