Solution to closing the ie window and clearing the Session

Source: Internet
Author: User
Tags server website

Copy codeThe Code is as follows:
// Function window. onunload () {alert ('This is what you want to do. It is done after you close the webpage! '); Location = 'sessionclear. aspx ';}
// Function window. onbeforeunload () {alert ('This is what you have to do. It was done before closing the webpage! ')}

Function window. onunload (){

If (window. screenLeft> = 10000 & window. screenTop> = 10000) | event. altKey ){
Alert (window. screenLeft + "," + window. screenTop );
// The Action to be triggered when the user shuts down abnormally
Location = 'handler1. ashx ';
}
}

</Script>

Handler1.ashx:
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
Using System. Web. SessionState;

Namespace WebApplication1
{
/// <Summary>
/// $ Codebehindclassname $ abstract description
/// </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:

Generally, a website in the form of a member creates a session or Cookie after the member logs in. Then, the member needs to exit the connection or click the button to exit when the member exits. When a member closes the form directly, a series of exits related to exit are not triggered. These will not be cleared until the server session expires.

Fortunately, on the network, we finally found a way to capture events that users can use Alt + F4, right-click the title bar, double-click the title bar, and directly press the close button. Of course, it cannot be captured to minimize to the taskbar and then close it.
Copy codeThe Code is as follows:
<Script language = "javascript">
<! --
Function window. onunload (){
If (window. screenLeft> = 10000 & window. screenTop> = 10000) | event. altKey)
{
// The Action to be triggered when the user shuts down abnormally
}
}
-->
</Script>

Note:
Window. screenLeft = 10000 + Border width (2 × 2) = 10004
Window. screenTop = 10000 + toolbar height + Title Bar Height = 10097

Note that in onBeforeUnload, these coordinate attributes are normal values.

ScreenLeft: obtains the x coordinates in the upper left corner of the browser's client area relative to the upper left corner of the screen.

ScreenTop: Obtain the y coordinate in the upper left corner of the browser client area relative to the upper left corner of the screen.

I guess that a special value will be generated when the form is destroyed. Under normal circumstances, click to test, the value will not exceed this value.

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

We can write this statement to replace a series of window. location. Because the portal involves multiple cross-server website servers. After exiting the unified portal, you must exit sequentially to achieve the expected effect of the portal website.

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

This section of code has been tested. Do not use window. close in onUnload because the event is triggered immediately before the object is destroyed. OnBeforeUnload is the event triggered before the page is to be detached.

In essence, the so-called clearing is to call the page that completes the exit function directly by opening a new window. The call may pause for a second or two when it is closed, or close the window and place it on the exit page. This page is switched back to the homepage after exiting the page. The difference is that the page will be closed automatically after exiting and can be opened directly without additional control.

[Note] If no judgment is added in window. onUnload, the event will be triggered when the current page changes, such as refreshing the page. Therefore, some normal operations must be blocked only when specific operations are captured.

Continued: in IE, how can I clear the Session before the user closes the window directly?

I explained the implementation idea yesterday, but in practice, I found that the program can be executed normally only after a delay is added. See the implementation details and a simple time delay function. The Code has been tested.
Copy codeThe Code is as follows:
<Script language = "javascript">
Function window. onUnload ()
{
Var newWindow;
If (window. screenLeft> = 10000 & window. screenTop> = 10000) | event. altKey)
{
NewWindow = window. open ('exit program address', 'webpage name ',
'Width = 0, height = 0, top = 4000, left = 4000 '); // the new window will be opened outside the view area.
NewWindow. opener = null;
Sleep (5000 );
NewWindow. close (); // close the new window
}
}
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; // Special case of minute-to-second during import
Gaps = hourGaps * 3600 + minuteGaps * 60 + secondGaps;
Gaps = gaps * 1000 + msGaps;
} While (gaps <milisecond );
}
</Script>

2. When the window is loaded or exited, you can perform the following operations to refresh the browser:
<Script type = "text/javascript" language = "javascript">
Zookeeper opener.doc ument. location. reload ();
</Script>
<Boey onload = "opener. location. reload ();"> refresh when opening a window
<Body onUnload = "opener. location. reload ();"> refresh when disabled

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.