SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onunload alert only if user leaves site

    So, I know that onunload fires when the page refreshes or the user navigates to another page or when the window is closed. What I want to know is this: can I use the unload event to open an alert box only when the user is leaving my site or has closed the window? (This is for use on a VPN to alert the user that they need to close the VPN before they can navigate to another site.)

    I'm thinking that it would be along lines similar to this:
    Code:
    function vpnUnload()
    {
        if(window.closed || locationIsNotOnVPN)
        {
            alert("You must close the VPN connection before you can leave this site!");
        }
    }
    It's the locationIsNotOnVPN that I'm not sure about. Checking window.location shows the current URL, not the one the user is moving to. This alert needs to show when the user navigates outside of a specific (and small) set of pages, but must not show when the user moves within these pages nor when the user refreshes the page. Is this possible to do?
    PHP questions? RTFM
    MySQL questions? RTFM

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't detect when they are closing the window, except in IE (see this thread). In that thread I had an idea based on using something like what this demo does. It would allow you to look at any href before it becomes the new location.

  3. #3
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Mike. I may be able to adapt your Link Interception method to my needs - the only hitch that leaves is page refreshes. May not even implement this, and just rely on user training instead (Ha!!). Thanks for the info, though!
    PHP questions? RTFM
    MySQL questions? RTFM

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster View Post
    You can't detect when they are closing the window, except in IE
    window.onbeforeunload is also supported in Firefox

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good info, Pepe And it seems to work just like it does in IE.

    For reference: IE onbeforeunload

    I could not find any FF/Mozilla documentation on this event.


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
  •