Jump to content
Larry Ullman's Book Forums
Sign in to follow this  

Recommended Posts

Hi,

 

I have a question about Creating Modal Windows example... Quote:

 

 

function openModal() {
    'use strict';
    document.getElementById('closeModal').onclick = closeModal;
    document.getElementById('modal').style.display = 'inline-block';
    document.getElementById('openModal').onclick = null;
}

function closeModal() {
    'use strict';
    document.getElementById('openModal').onclick = openModal;
    document.getElementById('modal').style.display = 'none';
    document.getElementById('closeModal').onclick = null;
}

window.onload = function() {
    'use strict';
    document.getElementById('openModal').onclick = openModal;
};

 

If I remove the following lines (altogether, all three lines):

  • In function openModal(): document.getElementById('openModal').onclick = null;
  • In function closeModal(): document.getElementById('openModal').onclick = openModal;
  • In function closeModal(): document.getElementById('closeModal').onclick = null;

so that the new code looks like this:

 

 

function openModal() {
    'use strict';
    document.getElementById('closeModal').onclick = closeModal;
    document.getElementById('modal').style.display = 'inline-block';
}

function closeModal() {
    'use strict';
    document.getElementById('modal').style.display = 'none';
}

window.onload = function() {
    'use strict';
    document.getElementById('openModal').onclick = openModal;
};

 

everything works well, there are no errors. I don't understand why there are these three lines?

 

Thank you in advance!

Share this post


Link to post
Share on other sites

The point of those lines is to drop event handlers when they're not needed. This is a tidiness issue, that's not required, and won't affect the functionality, but is arguably more professional and can prevent memory leaks.

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...