Home > Web Front-end > JS Tutorial > body text

Solution to closing IE window and clearing Session_javascript skills

WBOY
Release: 2016-05-16 17:04:35
Original
1540 people have browsed it

Copy code The code is as follows:

//function window.onunload() { alert(' This is what you have to do, close the web page! '); location = 'SessionClear.aspx'; }
//function window.onbeforeunload() { alert('This is what you have to do, close The page was made before! ') }

function window.onunload() {

" // Actions that need to be triggered when the user closes abnormally

Location = 'Handler1.ashx';
}
}


Handler1.ashx:



Copy code The 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 description of $codebehindclassname$
///


[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-based websites will establish a session or cookie after the member logs in, and then the member needs to click on the exit link or button to exit. When the member directly closes the form, a series of exits involving exits is not triggered. These will not be cleared until the server session expires.
Fortunately, I finally found a method on the Internet that can capture events when the user uses Alt F4, right-clicks the title bar to close, double-clicks the title bar, or directly presses the close button. Of course, minimizing to the taskbar and then closing it cannot be captured.


Copy code The code is as follows:


Instructions:
window.screenLeft = 10000 border width (2×2) = 10004
window.screenTop = 10000 toolbar height title bar height = 10097

It should be noted that in onBeforeUnload, these coordinate attributes of the screen are normal values.

screenLeft: Gets the x-coordinate of the upper left corner of the browser client area relative to the upper left corner of the screen.

screenTop: Gets the y coordinate of 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. In a click test under normal circumstances, the value will not exceed this value.

The problem now is that using window.location in onBeforeUnload can submit the request to the specified URL normally, 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 something like this to replace the series of window.locations used in the past. Because the portal involves multiple cross-server website servers. After exiting from the unified entrance, you need to exit sequentially in order to achieve the desired 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 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 an event triggered before the page is unloaded.

The so-called clearing is essentially to call the page with the exit function directly by opening a new window. There may be a pause for a second or two when the call is made to close, or the window may be closed on a dedicated exit page. The difference between this page and normal exit and switching back to the homepage is that it will be automatically closed after exiting and can be opened directly without additional control.

[Note] If no judgment is made in window.onUnload, this event will be triggered when the current page changes, such as refreshing the page. Therefore, judgment must be made to capture specific operations in order to block some normal operations.

Continued: How to clear the Session before the user directly closes the window in IE

I explained the implementation idea yesterday, but in actual operation I found that a delay must be added to ensure that the program can be executed normally. The implementation details are attached below, along with a simple time delay function. The code has been tested.

Copy code The code is as follows:



Second, when the window loads or exits, if you want to refresh the browser once, you can do the following:

Refresh when the window is open
Refresh on close
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template