Javascript – How to popup when a visitor exits from a website


My company creates web surveys and once in a while I get asked if we can do an exit survey on a website. So when a visitor is about to leave the site a "popup" appears asking if they'd take a quick survey. By "popup" I'm referring to a floating CSS div, not a child window (although sometimes clients don't realise this).

Is it technically possible to code a genuine exit survey?

I don't think so but am I missing something? The onbeforeunload event is close to the functionality required but the message and buttons aren't fully customiseable to make it usable.

The current trend for implementing an exit survey is to popup an invite at the start of the visit, and if the user says ok then a child window is opened and focus set back to the main window. At the end of the visit the users sees the child window. I swear I've never seen a "popup on exit" survey (unless by coincidence).

Best Solution

Surely they 'exit' the site by closing the window/tab? You're asking for a way to prevent them closing the tab? Or popup a window triggered by the close action? That sounds like it would be prevented by the browser for good reason, and if you found a workaround it would be likely to be fixed in future versions.