Popup when a user exits your site
This can be achieved using JavaScript, but it's a little tricky.
Most articles on the subject say that this is impossible without using frames, but there is a little javascript hack that you can use. This hack works in both IE and Firefox browsers.
The first step is to use the javascript onUnload event on your body element as so:
<body onUnload="leave()">
The unfortunate effect of this is that the leave() function is not only called when a user exits the site, but each time a user visits a new page within the site. So we can't simply open a popup each time this event is called. Or can we?
Let's create the leave() function:
<script>
function leave() {
window.open('popup.html','exitpage','height=1,width=1,top=' + screen.height + ',left=' + screen.width +',toolbar=0');
}
</script>
By initially setting the top and left attributes to screen.height and screen.width, the pop will be displayed off of the screen. This way the user won't notice anything going on.
Since this will happen each time a user visits a page within the site, we want to close the popup window immediately if the user has simply gone to another page. If the user has left the site however, we want to move that popup to the front and center of the screen. Put this code at the top of your popup.cfm page:
<script>
try {
if (window.opener.location.href == '')
window.close();
else
window.moveTo(1,1);
top.resizeTo(720,725);
}
catch (err) {
window.moveTo(1,1);
top.resizeTo(720,725);
}
</script>
If you try to call window.opener.location.href after the user has closed the browser or gone to a different site most browsers will throw an error. We will catch that error and use it to move the window and resize it appropriately.
Need assistance with your project? Universal Web Services can help.
Contact us to request a quote.