What is difference between these two codes ?
$(document).on(\'click\', \'.close_modal_window\', function(){
alert(\"window closed\");
});
In your first example you are putting on click event to document, not to the modal window itself. Reason your first block of code works is because it works even if you add elements dynamically later. If you want your second block of code to work as well, you need to make sure that you already have the HTML fully loaded before trying to add click event.
Check out .on() documentation
The way I figure it for myself is that your first block of code is going to put click event on whole document.
Your second block of code does the same thing but differently.
Also in this case if you add more classes with same name, it will not work unless you run this part of code again, so you have to be careful if you plan on adding more HTML elements with same class and have the click working on it