I'm assuming what you want to do is show some sort of pop up when the user indicates intent to leave.
This can be achieved by using the javascript 'mouseout' event. I.e. if the mouse leaves the browser window (say user goes to close a tab, the window, or click on another tab, etc.) then your survey is fired.
Obviously doesn't always work - the user might be trying for example to click on a browser menu item in which case your survey would be fired. Also I do not believe it would work on touch devices.
But if you can live with those types of kinks, here's the code you need.
// Set up an event listener to capture exit intent
function addEvent(obj, evt, fn) {
if (obj.addEventListener) {
obj.addEventListener(evt, fn, false);
}
else if (obj.attachEvent) {
obj.attachEvent("on" + evt, fn);
}
}
// Trigger exit intent on mouseout event
addEvent(document, 'mouseout', function(evt) {
if (evt.toElement == null && evt.relatedTarget == null ) {
console.log('Let's do some magic here');
};
});