Capture the close event of popup window in JavaScript

xiaohan2012 picture xiaohan2012 · Feb 22, 2012 · Viewed 124.2k times · Source

I need to perform some action before a popup window(using window.open ) closes.

Something like will be good:

var new_window = window.open('some url')
new_window.onBeforeUnload = function(){ my code}

How can I achieve that?

Answer

glortho picture glortho · Feb 22, 2012

Your example will work as long as the pop-up window url is in the same domain as the parent page, and you change the event to all lowercase:

var new_window = window.open('some url')
new_window.onbeforeunload = function(){ /* my code */ }