Web Analytics Made Easy -
StatCounter Alert on page exit - CodingForum

Announcement

Collapse
No announcement yet.

Alert on page exit

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Alert on page exit

    Hello All

    I have a logout button that needs to be pressed before a user exits the page, so how do I create an alert when the user tries to exit the page without clicking the logout button? Something that will ask the user if he/she has pressed the logout button before exiting the page, if so go ahead and press yes and the page will exit, or if no the user will be brought back to the page. Hope I was clear. Thanks.

    Tone

  • #2
    Hello,

    I don't think it's possible to prevent the user from leaving your page just because they click cancel. However, I think this should work well as an alternative (this is untested)...

    In your <BODY> tag, add:
    Code:
    onUnLoad="confirmLeave();"
    Right after your <BODY> tag, add:
    Code:
    <script language="JavaScript">
    
    var logout;
    
    function confirmLeave () {
        if (logout!==1) {
            alert("You have not logged out, it is recommended that you click the back button on your browser and logout for your own safety!");
        }
    }
    
    function logOut () {
        logout = 1;
    }
    
    </script>
    Then on your Logout button, add:
    Code:
    onClick="logOut();"
    However, this leads to complications because what if they are surfing to another page, but is still in what I presume to be an account manager?

    Comment


    • #3
      That won't stop the window from closing or going to another page. IE has onbeforeunload event which can cancel navigating away from the page. But of course, it's IE only.
      Glenn
      vBulletin Mods That Rock!

      Comment


      • #4
        so then what would be a solution?

        Comment


        • #5
          Originally posted by glenngv
          That won't stop the window from closing or going to another page.
          I never said it would, I said it's a possible alternative.

          Comment


          • #6
            How would you consider that as an alternative solution when it won't solve the problem at all?

            Using your alternative solution...If the user clicks File-Close from the menu or the X button, the user will still be able to exit without clicking the logout button.
            Glenn
            vBulletin Mods That Rock!

            Comment


            • #7
              so is there a solution for this kind of issue?

              tone

              Comment


              • #8
                What server-side language you use?
                Is this for intranet or internet?
                Glenn
                vBulletin Mods That Rock!

                Comment


                • #9
                  .net

                  Comment


                  • #10
                    internet

                    Comment


                    • #11
                      Just like classic ASP, .NET has global.asax that contains the Session_OnEnd event handler that is triggered when Session.Abandon is executed or when session times out.

                      When the user closes the browser without logging out, the server waits for the session time out to expire to execute the code in Session_OnEnd where you can have whatever clean up you want to do.

                      There was an extensive discussion on this topic here which I also participated in. This is in classic ASP though, I don't know if there's already a solution for .NET.
                      Glenn
                      vBulletin Mods That Rock!

                      Comment


                      • #12
                        thanks i'll look into it. do u have an email in case i have further inquiries?

                        Comment


                        • #13
                          Just post your questions in this thread.
                          I think this should be moved to ASP forum.
                          Glenn
                          vBulletin Mods That Rock!

                          Comment


                          • #14
                            thanks for the thread glenn, i found it very helpful. So all in all, there still isn't really a solution to this problem because u still have to wait 20 minutes after someone exits your app. the session doesn't end immediately, right?

                            Comment


                            • #15
                              Right. Actually, 20 minutes after the last server request, not necessarily when application is exited.
                              Glenn
                              vBulletin Mods That Rock!

                              Comment

                              Working...
                              X