Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: On page exit javascript confirm

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default On page exit javascript confirm

    Hi everyone,
    I'm looking for a script that will make a confirm box when someone try's to exit the page. If the select yes, then it exits. If they select no, then it cancels the exit.

    Any help?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    http://www.google.com/search?q=onunload+confirm


    But use it carefully: these scripts are very annoying.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Thanks for your help. I found this code


    HTML Code:
    <body onunload="alert('The onunload event was triggered')">
     </body>
    When I refresh the page the alert box comes up but when I exit it, nothing happens? Any help?

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Are you using IE? I don't think the unonload event works in IE. It works in Chrome and Firefox though, I know that for sure.
    - Mike

  5. #5
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Generally when you see that kind of thing, pop up messages trying to prevent you from leaving, are associated with spam and untrustworthy content. Like Daniel said, be careful with it.
    - Mike

  7. #7
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    I'd like to use it to tell advise them to log off (if they arn't already) before leaving the page.

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    If your website uses sessions, they will expire over time anyways. But I see your predicament. Maybe you need to look into something like cron jobs, or something. Is there any important information stored in the sessions? If not, there shouldn't be too much of an issue.

    On a side note, you could still do the unonload thing, but it would only work in some browsers:

    Code:
    <body onunload="if (confirm("You may need to log out before you leave.")) return false; ">
    - Mike

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Actually under some testing, that doesn't work either. Might take a little bit of digging to find the answer to this one.
    - Mike

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Try putting this in the head of your page:

    Code:
    <script type="text/javascript">
    function confirm(){
      return "You need to log out.";
    }
    window.onbeforeunload = confirm;
    </script>
    - Mike

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
  •