How to find out the life of a script or javascript block?

I am wondering if a javascript block / function is ever loaded. Because I tested something, and now I'm a little confused. I defined a script block in a div. The script block has an event handling function for an element that reloads the div using ajax. Ajax call returns a simple html div and replaces it with the current one. But that means replacing a script that also does execution. I decided that the script would be disconnected from execution after the replace statement. But this is not so. Lines of code after executing the replace statement are made in the way that works. How do you describe the lifetime of a script block?

+5
source share
1 answer

When the code contained in the element scriptis evaluated, the result of this code evaluation becomes part of the page runtime. Removing an element scriptdoes not remove the resulting structures (functions, etc.) from the environment.

So, if a script defines functions or intercepts event handlers for elements or creates new properties for existing objects (including a global object), these functions, handlers and properties remain in memory, even if scriptcertain of them are deleted from the DOM (as usual JavaScript garbage collection, for example, objects that are not referenced anywhere are eligible for the GC, but the element scripthas nothing to do with it). An element scriptis just a mechanism for passing code to a browser.

+8
source

All Articles