SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Central Illinois
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alert box when leaving page

    I've found a lot of scripts out there that will display an alert box when a visitor closes their browser or tries to leave your site. Personally I find that a really annoying tactic, and virtually useless, but I have now found myself in a position where such a thing really makes sense to use. Unfortunately, I can't find a ready-made script that meets the specifications I need.

    I need the script to recognize when the browser window is being closed, or when the visitor tries to navigate to a different domain name than mine. I'd want to have a "OK" and "Cancel" button available. Clicking "OK" sends them to specific URL, regardless of where they were headed or if they were closing their browser. Clicking "Cancel" lets them continue as they were.

    Does anyone know of such an animal? I've looked all over for one, but haven't found one that will do all that. And I sure don't have the JS skills to write my own!

    Thanks,

    Matthew

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    May be you are looking for the onBeforeUnload event.
    Example of use:

    HTML Code:
    <script type="text/javascript">
    
    function unloadHandler ( ) {
      //code
    }
    
    window.onbeforeunload = unloadHandler();
    </script>

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Central Illinois
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, this is a start, but I'm still a little confused as to how to recognize when the domain is changing. Basically I'm using this as a reminder for people to logout of their accounts on my site, so they need to be able to view any page on my site without any warning, but get the confirm box if they try to leave the site. How do I check for when they're leaving my domain, before they've actually left?

    Thanks again,

    Matthew

  4. #4
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats a major league tough one ... the onbeforeunload works BUT it fires any time you change from page to page SO you would have to check WHAT link they have clicked on (if any) test whether its your domain or not and then if it isnt throw the alert up....... real pain in the you know what!

    What are you doing your logins with? Reason I ask is that I have had this problem with membership (login) type sites and have never found the perfect solution via javascript.

    What I always end up doing is adding a handler to the sessions (in Java its easy, there is a class called SessionListener just for that) ... anyways the session listener grabs the users data when the session expires and logs them out. In Java its set to fire right when the session dies and can handle any type code you wish (IE: going to the db and setting their row as logged out)

    Im not sure if the other languages have anything like that but Id be willing to bet they might ..... just a thought!

  5. #5
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically, you'd have to have an event handler attached to all your page's links that would open a confirm() window and allow the okay/cancel action. It is not possible to do this for a URL typed into the address bar or a bookmark link, since your script will never see the event, though an alert (not a confirm) could be launched by onunload. The last I knew, onbeforeunload was only supported by a few browsers.


Bookmarks

Posting Permissions

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