Reputation: 11545
If I make ajax requests that remove the body HTML and append new HTML, do I need to also remove any event handlers that were added to the previous HTML?
I noticed that if I don't, everything works fine. Does the browser free the memory and stuff? What if I do thousands of such ajax requests without refreshing the browser? Will I get memory leaks?
Upvotes: 4
Views: 319
Reputation: 382092
If you add jQuery event handlers :
On most function documentations, you have a comment similar to this one :
When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.
If you're coherent, you'll have no memory leak, you don't have to manually remove data or event handlers. There's usually no problem in having a page kept open for days and issuing thousands of Ajax requests and changing the screen accordingly.
Upvotes: 2