Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

javascript - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...