0

I have this situation where I have to delete a script tag from the DOM with only the possibility of using another script for this. I know this isn't a good way of working, but it's the only option at this time.

Therefor my question is if this is possible. I think that when I delete this script you will still be able to use the functions inside.

Thanks!

2
  • 1
    what do you want to achieve with this Commented Mar 27, 2013 at 15:27
  • 1
    Deleting a <script> tag won't do you much good, because once the script is evaluated the tag itself doesn't do anything. The modifications the script made to the global page environment remain, and removing the tag won't affect those. Commented Mar 27, 2013 at 15:27

1 Answer 1

2

Is it possible to delete a script tag from the DOM by using another script for this.

Yes, you can remove <script> nodes from the DOM as you can modify any other nodes as well. You even can write a script that removes it's own element.

I think that when I delete this script you will still be able to use the functions inside.

That is correct. When the script has already been executed, the modifications it made to the environment will remain. You can't undo them other than by unloading the page (or explicitly overwriting them - if possible).

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

1 Comment

if each 'script' adds listeners you will need to remove them, and for functions and variables if you use a namespace you can just null that.

Your Answer

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