Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Events and DOM Elements

Let's assume that I have a page with a two column layout where the left column is a set of links and it loads the associated html page/template in the right column when that link is clicked.

Upon loading a template, there's a template handler that gets initialized as a singleton via requirejs and it defines some methods and handlers like:

SomePage.prototype.saveHandler: function(e) { ... }; // Page handler has a handler

SomePage.prototype.initialize: function() {
    $('#btnSave').on('click', saveHandler);
}

Then, I'm attaching DOM events through an initialize method every time the page loads.

SomePage.initialize(); // This attaches the click event

Now, when I click another link on the left, a different template page is loaded and the above process repeats for that page.

I'm wondering what happens to the click event that was attached earlier to the btnSave element? Is it now a dangling event handler in the jQuery cache?

If I try to remove it when the same page loads again, will it actually remove the original event?

$('#btnSave').off('click', saveHandler);

Does executing the following block prevent memory leaks / dangling references?

// The potential problem here is that btnSave is part of the newly loaded template 
// and not the element i attached the handler to earlier (which doesn't exist anymore)
$('#btnSave').off('click', saveHandler);

$('#btnSave').on('click', saveHandler); 

What is the best way to ensure I don't end up with dangling references and memory leaks. This is a potential question for plain javascript too. My understanding is that the above will not work when the template page is refreshed. I am going experiment with it in the meanwhile, but it would be nice to know how the experts handle this. Thanks!

like image 981
Nick Avatar asked Sep 01 '14 18:09

Nick


People also ask

What are DOM elements?

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.

What is event and what is DOM event?

HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document. Events are normally used in combination with functions, and the function will not be executed before the event occurs (such as when a user clicks a button).

How do you use event and DOM?

To provide a dynamic interface to a webpage, we use events in JavaScript . These events are attached to elements in the DOM ( Document Object Model ). By default, events use bubbling propagation i.e from children to parent. Events can be bound either as inline or in the external script (JavaScript file).


1 Answers

Have a look at this slide from Andy Osmani a javascript guru. You will find the answer about memory leak and circular references here: https://speakerdeck.com/addyosmani/javascript-memory-management-masterclass

like image 187
albanx Avatar answered Oct 24 '22 16:10

albanx