3

I have a script abc.js, which has an event handler for a button as follows:

$("button").on("click", function(e){
     $("script[src='abc.js']").remove();
});

what happens when the remove on the executing script is called? Will the script be unloaded, or will it stop executing? And what happens when the script is removed from other script file?

4
  • 2
    Will the script be unloaded, or will it stop executing? Nope, since script tag is just a way to load the script and JS engine doesn't check the presence of script tag before executing a statement. Commented May 23, 2016 at 11:57
  • would cause in error..... Commented May 23, 2016 at 11:58
  • $("<script src='abc.js'></script>").remove(); does not remove an executing script tag, it creates a new <script> tag with the abc.js as src, but this tag was never added to the DOM, so remove() does not have an effect anyway. Commented May 23, 2016 at 12:22
  • Ok, so I have edited selector passed to jQuery in my question. Commented May 23, 2016 at 12:30

2 Answers 2

8

Will the script be unloaded, or will it stop executing?

Nope, since script tag is just a way to load the script and JS engine doesn't check/validate the presence of script tag before executing a statement.

So, presence of script tag is not relevant once the script has been loaded to the session.

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

Comments

0

Once you have removed the script file the handlers on it will not be called as the function will no longer exist .

1 Comment

Removing a script tag does not have any effect on the already loaded and evaluated script. The script tag is only initially needed to trigger the script load/evaluation, after that the presence of the script tag is not relevant anymore.

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.