Kushagra Singh Rajput
Kushagra Singh Rajput

Reputation: 299

detecting window close event when opened through window.open

I am opening a window through window.open method. This window is for conducting test. Can i know how to detect the event when user closes the popup window. I want to detect this in popup window only not parent window.I mean kind of ajax request to insert a value in database when user closes it. I have tried onbeforeunload but it does not works. Please help me

Upvotes: 2

Views: 2975

Answers (2)

Andi AR
Andi AR

Reputation: 2898

Pretty simple try this

$(window).on('beforeunload', function() { 
return 'Are you sure want to logout ?'; 
});

$(window).unload(function() { 
debugger;    // when closing window debugger will hit here
});

Upvotes: 1

Sim
Sim

Reputation: 3317

If you want to know when a window or tab is closed then use unload or onbeforeunload, these events are fired too, when you leave the site by a link.

The API for jQuery unload(): https://api.jquery.com/unload/

...and pure javascript: https://developer.mozilla.org/de/docs/Web/API/WindowEventHandlers/onbeforeunload

Upvotes: 0

Related Questions