View Full Version : How to popup message to user when they are changing sites/closing window?

06-11-2008, 03:58 PM

I am trying to create code for an ecommerce site that will prompt the user to take a survey if they are leaving the site or closing the window, but not if they are navigating within the page.

I have seen code using the onbeforeunload event, but this seems to trigger in all situations where the page is changed, even if they are going to another page in the same site - which is not desired.

Is there a way to have the popup appear only if the user is closing the window or leaving the site?

06-11-2008, 04:07 PM
you could write a code so that every link, the close buton and anything that refreshes the page opens a alert window or you could use a div in CSS

I think you should use alert. that promps the user with a yes or no question.

I'm guesing you my have to connect the script to link that lead away from your page and when the user closes the window. I don't know if there is anything for when the user use the back button.

Philip M
06-11-2008, 08:14 PM
Try this:-

<body onbeforeunload = "annoyUser()">

<a href="http://www.yourdomain.com/anotherPage.html" onclick = "blockPopUp()" > Click here to go to another page</a>

<script type = "text/javascript">

var exitFlag = 0;
function annoyUser(){
if (exitFlag == 0) {
alert ("Showing the Annoying Pop-Up Here")

function blockPopUp () {
exitFlag = 1;
alert ("You are moving to another page without having to see the annoying pop-up!");


06-11-2008, 10:54 PM
Nobody wants to take a survey when they are leaving a page.

You could offer it in a link on the page, or on submitting an order-
maybe with a 'take the survey and get 5&#37; off your purchase' teaser.

Or it could be a 'splash screen' onload that a user can skip.