Need help understanding piece of Javascript code

Last post 05-09-2008 12:48 PM by NC01. 7 replies.

Sort Posts:

  • Need help understanding piece of Javascript code

    05-09-2008, 9:36 AM
    • Loading...
    • JZoerman
    • Joined on 08-24-2006, 2:27 AM
    • Posts 95

     Hello all,

    I have an http chat page in which I have simulated a persistent communication enviornment. I discovered the javascript code I had been using to detect when a person left the chat page was not working when a user closed out their browser. I found the code below to correct this problem and from a functional standpoint it works as desired, well almost. The message the user receives upon exiting the chat page is not as I would like. I need help understanding how to modify my javascript so that one of two things happens.

    First, a message that simply indicates to the user that they are being logged out with OK only, no cancel option and not the first line of the message "Are you sure you want to navigate away from this page?" An acceptable second option, leave as is and if the user does click cancel, how do I prevent their browser from unloading. Right now clicking Cancel has the same effect as OK, please note I switched to this code after discovering the original code did not detect browser unloading but only navigating from a page.

     

    Relevant code

    window.onbeforeunload = function (evt)
    {
    var message = 'You will be logged out of the chat room';
    if (typeof evt == 'undefined')
    {
    evt = window.
    event;
    }
    if (evt)
    {
    evt.returnValue = message;
    Leaving();
    }
    }

    Thanks in advance...

  • Re: Need help understanding piece of Javascript code

    05-09-2008, 10:00 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 6,867
    • TrustedFriends-MVPs

    You can't change the "Are you sure you want to navigate away from this page?" or the "Press OK to continue, or Cancel to stay on the current page" parts. The only thing that you have control over is the middle part of the message.

    I just tested this piece of code and it seems to work.

    <script type="text/JavaScript">
    <!--
    var g_isOkToClose = false;

    window.onbeforeunload = function ()
    {
     if ( g_isOkToClose == true )
      return;

     var closeMessage =
      'You will be logged out of the chat room\n' +
      'Are you sure you want to exit?';

     if ( window.event )
     {
      // IE only...

      window.event.returnValue = closeMessage;
     }
     else
     {
      // Other browsers NOTE: Not tested...

      return closeMessage;
     }

     g_isOkToClose = false;
    }
    // -->
    </script>

    Add to the CodeBehind, PageLoad event handler to stop the message from showing on PostBacks/submits:
     Page.RegisterOnSubmitStatement("OnSubmitScript", "g_isOkToClose = true;");

    NC...

  • Re: Need help understanding piece of Javascript code

    05-09-2008, 11:02 AM
    • Loading...
    • JZoerman
    • Joined on 08-24-2006, 2:27 AM
    • Posts 95

    I need to call another function when the user clicks OK, how do I determine which button was clicked? In fact my thought process here could be a little screwy, the imperative goal here is to run a SQL server delete statement when a user navigates away from the my page or unloads their browser. I don't necessarily have to notify the user what is happening.

  • Re: Need help understanding piece of Javascript code

    05-09-2008, 11:13 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 6,867
    • TrustedFriends-MVPs

    The only way to execute an SQL statement is from server-side and you are on the client-side at that point. There really is no sure way to do what you want, since there are so many ways that the user can close his browser to your application with no notification to you.

    You might try using the Session_End event in the Global.asx file. The Session End event is fired when the Session time expires. Session End will not be fired necessarily when you close the browser, but that will eventually make the Session time expire (depending on the application's timeout value). There are instances when it is never fired though.

    Here is an article showing a problem on a very similar situation to yours.
         http://dotnetslackers.com/community/blogs/haissam/archive/2007/07/02/Using-variables-in-Session_5F00_End.aspx

    The best idea is to create a Windows Service and install it on the server to SQL statement by a creation date, and have it run periodically.

    NC...

  • Re: Need help understanding piece of Javascript code

    05-09-2008, 11:39 AM
    • Loading...
    • JZoerman
    • Joined on 08-24-2006, 2:27 AM
    • Posts 95

    I am not having a problem with the SQL statement, I have a javascript function Leaving() that calls a sub from my code behind to log out the user and that works fine. The problem is I only want to call the javascript function Leaving() when OK is clicked and ignore it when cancel is clicked.

    I definately don't like Session_End for several reasons such as you mentioned. If I can figure out how to call my javascript function Leaving() when OK is clicked and not when Cancel is clicked my problem will be solved.

    BTW, thanks for the quick responses.

  • Re: Need help understanding piece of Javascript code

    05-09-2008, 11:48 AM
    Answer
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 6,867
    • TrustedFriends-MVPs

    Handle the window.onunload event also which will only be fired if the window actually closes.

    <script type="text/JavaScript">
    <!--
    var g_isOkToClose = false;

    window.onbeforeunload = function ()
    {
     if ( g_isOkToClose == true )
      return;

     var closeMessage =
      'You will be logged out of the chat room\n' +
      'Are you sure you want to exit?';

     if ( window.event )
     {
      // IE only...

      window.event.returnValue = closeMessage;
     }
     else
     {
      // Other browsers NOTE: Not tested...

      return closeMessage;
     }

     g_isOkToClose = false;
    }
    window.onunload = function ()
    {
     alert('window.onunload fired');
    }
    // -->
    </script>

    NC...

  • Re: Need help understanding piece of Javascript code

    05-09-2008, 12:18 PM
    • Loading...
    • JZoerman
    • Joined on 08-24-2006, 2:27 AM
    • Posts 95

    I was mistaken, any way to achieve this same principal during onbeforeunload? Your answer to my previous question was correct, but the statement I made was incorrect. It seems the call to my code behind cannot be made because the page has indeed unloaded.

    Any ideas for me? I hate to have to rethink the whole idea since my app is working nearly perfect, well except that blasted Safari but I was going to deal with that later.

  • Re: Need help understanding piece of Javascript code

    05-09-2008, 12:48 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 6,867
    • TrustedFriends-MVPs

    No because onbeforeunload fires before the window is onloaded and there you can just present the message stopping the unload.

    NC...

     

Page 1 of 1 (8 items)