Solved - user closes page need to reset session varaible...page_unload

Posted on 2011-05-07
Last Modified: 2012-08-13
When the user closes the page I need to reset the session variable....isnt page_unload used for this when hit the 'X" on the top right hand corner??
Question by:GlobaLevel
    LVL 13

    Expert Comment

    No, it isn't. The page_unload happens on the server side, while the page closes on the client side. There is no connection between both.

    Implementing your requirement is a bit complicated. It needs a way the client (web browser / page) to inform your server side application that ALL your application pages have been closed. This can be done in several ways:

    - Ajax request when the page closes: Whenever the user closes your page, it sends an ajax request to inform your app that the page has been closed and it will kill the session. But you will face a problem if there was several pages opened. The session will be killed if only one page is closed. You will need to find a way to fix this.

    - Polling the server: I used this method in one of our web applications. It gives a good result for both, detecting who is online and using your app, and who went offline or closed you pages.
    This approach consists of simply sending an ajax request from your page (client side) each interval (10 seconds maybe). The server receives this request and makes sure that the client has a page opened for your application. If the serve side app doesn't get a request for maybe 30 or more seconds, it can assume that all the pages to your application has been closed. The proble with this approach is that it will consider the page has been closed if the internet connection on the client side faced some trouble. This might cause some confusion for your user because the session expired although he didn't close the page.
    LVL 83

    Expert Comment

    LVL 4

    Accepted Solution

    i dont think u can ever capture the close event from the serverside.

    U can pop-open a new window from javascript, n in the page load of the poped up page, do ur coding n make the page close from there.

    Additionally, U can refer to my answer here:

    <head runat="server">
        <script type="text/javascript">
             function ConfirmClose() {
                if (event.clientY < 0) {
                    //event.returnValue = 'Any message you want';
    <body class="Resolution_1024x768" onbeforeunload="ConfirmClose()" >
    <form id="form1" runat="server">
    blah blah blah
    ********** page load of clearsession.aspx*********
        protected void Page_Load(object sender, EventArgs e)
            if (!Page.IsPostBack)
                  //ur Coding
            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "ClosePage();", true);
    ### ClosePage() function in clearsession.aspx###
                function ClosePage() {
          '', '_self', '');

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now