My case is not exactly this, but the concept is very similar, I have a content which get loaded via ajax, with a script inside.
page1.html:
<div id="ajax-content"></div>
<button onclick="loadContent()">do ajax</button>
<script>
function loadContent() {
$.get('page2.html', function(response) {
$("#ajax-content").html(response);
});
}
</script>
page2.html:
<button id="btn">button</button>
<script>
(function() {
var MyModule = {
main: function() {
$("#btn").click(function(){
alert("btn click");
// some code
});
}
// some more functions...
};
MyModule.main();
})();
</script>
Assume that the "do ajax" button will be clicked more than once.
[Each click will load content from the server, which I want to be refreshed frequently. ]
The loaded page (page2.html) has some interactive widgets in it, hence the script tag is get loaded and executed there.
As you can see, I put all the code in page2 in a self invoking function (module pattern), since I don't want to pollute the global scope.
Since each ajax request led to call the script again, MyModule
is created multiple times, and I'm not sure if the previous copy will get GC'ed.
So here I my thought of how this is happening, tell me if I'm wrong -
- button "do ajax" is pressed (page1.html), the function loads the contents of page2.html, and put the result inside the div.
- The self invoking function get executed, creates MyModule. The
main
function in my module get called - The
main
function bind an event handler to the button with id='btn'. so now we have these references: btn -> handler -> all of the self invoking function scope.
Now here is where I'm not sure: if I remove the button with id='btn' from the document, it's handler can be GC'ed => the entire self-invoking function scope will be GC'ed ?
If I'm correct, since each ajax call will replace the html of the div[id='ajax-content']
, no memory leak will happen.