<button id="modal_button">Open modal</button>
<div class="modal"></div>
var handler = function(e) {
if(e.target !== modalButton && !e.target.matches('.modal, .modal *')) {
modal.classList.remove('open');
this.removeEventListener(e.type, handler);
}
};
var modal = document.querySelector('.modal');
var modalButton = document.getElementById('modal_button');
modalButton.onclick = function() {
if(!modal.classList.contains('open')) {
modal.classList.add('open');
window.addEventListener('click', handler);
}
};