Close IE window to clear session resolution _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

function Window.onunload () {alert (' This is what you want to do, after closing the page! '); Location = ' sessionclear.aspx '; }
function Window.onbeforeunload () {alert (' This is what you want to do, before closing the page! ') }

function Window.onunload () {

if ((Window.screenleft >= 10000 && window.screentop >= 10000) | | event.altkey) {
Alert (window.screenleft+ "," +window.screentop);
Actions that need to be triggered when a user shuts down abnormally
Location = ' Handler1.ashx ';
}
}

</script>


HANDLER1.ASHX:
Copy Code code 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>
Summary description of $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;
}
}
}
}


Explain:

The general membership form of the site, after the member login will establish a session or cookie, and then need to quit the connection at the member exit or button out. When the member closes the form directly, there is no trigger that involves exiting a series of exits. These will not be erased until the server session expires.

Fortunately on the network finally found can capture the user to use Alt+f4, the title bar right-click to close, double-click the title bar, directly press the Close button of the event method. Of course, to minimize to the taskbar and then shut down is not captured.

Copy Code code as follows:

<script language= "JavaScript" >
<!--
function Window.onunload () {
if ((window.screenleft>=10000 && window.screentop>=10000) | | Event.altkey)
{
Actions that need to be triggered when a user shuts down abnormally
}
}
-->
</script>

Description
Window.screenleft = 10000 + Border width (2x2) = 10004
Window.screentop = 10000 + Toolbar height + title bar high = 10097

To be explained, in onbeforeunload, these coordinate properties are normal values.

Screenleft: Gets the x-coordinate of the upper-left corner of the browser's 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 suspect that capturing the form when it is destroyed will produce a special value. In the normal case of the hit test, the value will not exceed this value.

The problem now is that using window.location in onbeforeunload can properly commit the request to the specified URL, but the method does not execute effectively in the OnUnload event. The solution is to open a new window and close it off.

We can write these words to replace the window.location that we used in the past. Because the portal site involves multiple Web server servers across servers. After the unified entry exits, it is necessary to sequentially exit in order to achieve the desired effect of the portal site.

var NewWindow;
Window.opener=null;
Newwindow=window.open (url,pagename, ' height=0,width=0 ');
Newwindow.opener=null;
Newwindow.close ();
......

This code has been tested to not use Window.close in OnUnload, because the event is triggered immediately before the object is destroyed. The onbeforeunload is the event that the page will trigger before it is unloaded.

And the so-called purge is essentially a good exit function of the page, directly to open a new window way of the call. You may pause for two seconds when the call is closed, or close the window on a special exit page. The page and normal exit and cut back to the home page, the difference is to automatically shut down after the exit, do not need to control the other directly open it.

[note] If not judged in Window.onunload, the event will be triggered when the page is refreshed, and so on, causing the current page change. Therefore, it is necessary to judge and capture certain operations in order to block out some normal operations.

continued: In IE, how to clear the session before the user closes the window directly

Yesterday explained the implementation of ideas, but in the actual operation found that the need to add a delay to ensure that the program can be completed properly. The details of the implementation are attached below with a simple time delay function. The code has been tested.

Copy Code code 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 ', ' Web 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 close
}
}
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 ();

Todo
{
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 entering into seconds
gaps=hourgaps*3600+ minutegaps*60+ Secondgaps;
Gaps=gaps*1000+msgaps;
}while (Gaps<milisecond);
}
</script>


two, when the window is loaded or exited, want to let the browser refresh once you can do the following:
<script type= "Text/javascript" language= "JavaScript" >
Window.opener.document.location.reload ();
</script>
<boey onload= "Opener.location.reload ();" > Refresh when opening windows
<body onunload= "Opener.location.reload ();" Refresh when > Closes

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.