Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Thanked 0 Times in 0 Posts

    How to popup message to user when they are changing sites/closing window?


    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?

  • #2
    Regular Coder riptide's Avatar
    Join Date
    Jan 2007
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    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!");

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Thanked 132 Times in 123 Posts
    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.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts