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?
self. close(); should do it. That should work from within the popup.
In Javascript, popup boxes are used to display the message or notification to the user. There are three types of pop-up boxes in JavaScript namely Alert Box, Confirm Box and Prompt Box. Alert Box: It is used when a warning message is needed to be produced.
In this article, we will use jQuery Mobile Popup close() method to close an already opened popup. This method doesn't accept any parameter. Example: In the example below we will use Popup close() method to close the opened popup after 3000ms.
While the accepted answer is correct for same origins I found a solution for cross origin popups:
var win = window.open('http://www.google.com'); var timer = setInterval(function() { if(win.closed) { clearInterval(timer); alert('closed'); } }, 1000);
Source: atashbahar.com
For those considering using it.
Even Facebook is using this "hack" in their Javascript SDK.
You can verify this by having a look at their code. Just search for .closed
in https://connect.facebook.net/en_US/sdk.js.
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 */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With