Close the ie window to clear the Session solution

  • 2020-03-30 01:21:00
  • OfStack


//Function window.onunload() {alert(' this is what you do when you close the page! '); Location = 'SessionClear. Aspx'; }
        //Function window.onbeforeunload() {alert(' that's what you do before you close the page! ')}
        function window.onunload() {
            if ((window.screenLeft >= 10000 && window.screenTop >= 10000) || event.altKey) {
                alert(window.screenLeft+","+window.screenTop);
                //An action that needs to be triggered when the user is not normally closed
                location = 'Handler1.ashx';
            }
        } 
    </script>

Handler1. Ashx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
namespace WebApplication1
{
    /// <summary>
    /// $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler1 : IHttpHandler,IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Session.Abandon();
            context.Session.Clear();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Explanation:

General membership form of the website, after the member login will establish a session or Cookie, and then need to exit the connection when the member exit or press the button to exit. When the member closes the form directly, no series of exits involving the exit are triggered. These are not cleared until the server session expires.

Fortunately, a method was finally found on the network to capture the user using Alt+F4, right click on the title bar to close, double click on the title bar, and directly press the close button. Of course, minimizing to the taskbar and then closing it cannot be captured.


<script language="javascript"> 
<!-- 
function window.onunload(){ 
if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey) 
{ 
//An action that needs to be triggered when the user is not normally closed
} 
} 
--> 
</script> 

Description:
Window. screenLeft = 10000 + border width (2 x 2) = 10004
Window. screenTop = 10000 + toolbar height + title height = 10097

To be clear, these coordinate properties are normal values on the screen in onBeforeUnload.

ScreenLeft: gets the x coordinate of the top left corner of the browser client area relative to the top left corner of the screen.

ScreenTop: gets the y coordinate of the top left corner of the browser client area relative to the top left corner of the screen.

My guess is that the value captured when the form is destroyed will generate a special value. In the normal case of the click test, the value will not exceed the value.

Now the problem is that using window.location in onBeforeUnload can normally submit requests to the specified URL, but this method cannot execute effectively in the onUnload event. The solution is to open a new window and close it again.

So we can say, instead of using a series of window.location. Because portals involve multiple cross-server web servers. After the unified entrance exit, need to exit in order to achieve the desired effect of the portal site.

Var newWindow.
Window. The opener = null;
NewWindow = window. The open (URL, PageName, 'height = 0, width = 0');
NewWindow. Opener = null;
NewWindow. Close ();
...

This code has been tested. Do not use window.close in onUnload because the event will be triggered immediately before the object is destroyed. And onBeforeUnload is the event that is fired before the page will be unloaded.

And the so-called clean up in essence is to do a good job of the exit function of the page, directly to open a new window. You might be able to pause for a second or two when the call closes, or the closing window is placed on a special exit page. The page and the normal exit and cut back to the home page, the difference is that the exit will be automatically closed, do not need to control the open directly.

[note] if left unchecked in window.onunload, the event will be triggered when a page changes, such as a page refresh, etc. Therefore, you have to make a judgment to capture certain operations in order to mask some normal operations.

Continuation: how to clear Session in IE before the user closes the window directly

The implementation idea was explained yesterday, but in practice, it was found that a delay had to be added to ensure that the program could execute normally. The implementation details are attached below, along with a simple time delay function. The code has been tested.


<script language="javascript"> 
function window.onUnload() 
{ 
var newWindow; 
if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey) 
{ 
newWindow=window.open( 'exit program address ',' Web site name ', 
'width=0,height=0,top=4000,left=4000');//The new window will open outside the viewport
newWindow.opener=null; 
sleep(5000); 
newWindow.close();//New window closes
} 
} 
function sleep(milisecond) 
{ 
var currentDate,beginDate=new Date(); 
var beginHour,beginMinute,beginSecond,beginMs; 
var hourGaps,minuteGaps,secondGaps,msGaps,gaps; 
beginHour=beginDate.getHours(); 
beginMinute=beginDate.getMinutes(); 
beginSecond=beginDate.getSeconds(); 
beginMs=beginDate.getMilliseconds(); 
do 
{ 
currentDate=new Date(); 
hourGaps=currentDate.getHours() - beginHour; 
minuteGaps=currentDate.getMinutes() - beginMinute; 
secondGaps=currentDate.getSeconds() - beginSecond; 
msGaps=currentDate.getMilliseconds() - beginMs; 
if(hourGaps<0) hourGaps+=24; //Consider the special case of advancing time and minutes and seconds
gaps=hourGaps*3600+ minuteGaps*60+ secondGaps; 
gaps=gaps*1000+msGaps; 
}while(gaps<milisecond); 
} 
</script> 

Second, when the window loads or exits, the browser can be refreshed as follows:
< Script type = "text/javascript" language = "javascript" >
            Window. Opener. The document. The location. Reload ();
< / script>
< Boey onload = "opener. Location. Reload ();" >     Refresh when opening a window
< Body onUnload = "opener. Location. Reload ();" > Refresh when closed


Related articles: