Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does removing a script element remove its functions from memory?

var scripts  = document.getElementsByTagName("script");
for (var i=scripts.length; i--; ){
   (scripts[i]).parentNode.removeChild(scripts[i]);
}

Someone asked me this question and my first thought was: no. However, when you remove the style elements, the page automatically updates, removing the styling. This could be because of how the browser hooks css - I think I recall that CSS updates on every event (mouse movement, clicks, type, etc).

I just wanted to confirm, that getting rid of the script tag, won't get rid of the function that was already created, since I'm not at a computer where I can test.

This also has got me thinking of good practice to help secure code against firebug[-like] users

like image 219
vol7ron Avatar asked Jan 31 '12 16:01

vol7ron


People also ask

Can you remove a function in JavaScript?

No, you can not delete the result of a function declaration.

What is the use of script element?

<script>: The Script element. The <script> HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code. The <script> element can also be used with other languages, such as WebGL's GLSL shader programming language and JSON.

How do I remove a script in HTML?

Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

How to remove js file dynamically?

Dynamically removing an external JavaScript or CSS file To remove an external JavaScript or CSS file from a page, the key is to hunt them down first by traversing the DOM, then call DOM's removeChild() method to do the hit job.


1 Answers

Short answer, No.

Any script which got evaluated once by the engine will stay in memory for the rest of your session. Even by removing the entire script node where the code was contained doesn't change that fact.

like image 185
jAndy Avatar answered Oct 05 '22 07:10

jAndy