Close IE window to clear session resolution

Source: Internet
Author: User
Tags exit in relative sleep

  This article is mainly on the closure of the IE window to remove the session of the solution to the detailed introduction of the needs of friends can come to the reference, I hope to help you all

    Code as follows://function Window.onunload () {alert (' This is what you want to do, shut down the page and do it! '); Location = ' sessionclear.aspx '; }        //function window.onbeforeunload () {alert (' This is what you're going to do before closing the page! ')}           function window.onunload () {              if ( (Window.screenleft >= 10000 && window.screentop >= 10000) | | Event.altkey) {                alert (window.screenleft+ "," +window.screentop); &n Bsp              //user shutdown required action                 Location = ' Handler1.ashx ';                    }        </script> &nbsp ;   HANDLER1.ASHX: 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>    ///$codebehindclassname $ brief description    ///</SUMMARY&G T     [WebService (Namespace = "http://tempuri.org/")]     [webservicebinding (ConformsTo = Wsiprofiles.basicprofile1_1)]     public class Handler1:ihttphandler,irequiressessionstate     {&N Bsp         public void ProcessRequest (HttpContext context)         {    &N Bsp       context. Response.ContentType = "Text/plain";             context. Session.Abandon ();             context. Session.clear ();        }           public bool isreusable         {&nbs P           get             {          &NBS P     return false;            }         {   }}     interpretation:   General membership form of the site, after the member login will establish a session or cookie, and then need to be in the member Exit the connection or press the button to exit. 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, finally found on the network can capture the user to use Alt+f4, the title bar right-click 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.     Code as follows: <script language= "JavaScript" >  <!--  function window.onunload () {  if ( window.screenleft>=10000 && window.screentop>=10000) | | Event.altkey)   { //user needs to trigger actions  } } -->  </script>    Description when abnormal shutdown:   Window.screenleft = 10000 + Border width (2x2) = 10004  Window.screentop = 10000 + Toolbar High + title bar high = 10097    need to say Ming, in the onbeforeunload the screen these coordinate properties 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 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.     Now the problem is that using window.location in onbeforeunload can normally submit requests to the specified URL, but the method does not execute effectively in onunload events. The solution is to open a new window in theShut off.     We can write such words to replace the past, a series of window.location. 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, Do 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.     The so-called purge is essentially the page that completes the exit function, directly to open a new window in the 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: How to clear session  in IE before the user closes the window?   yesterday has explained the implementation of ideas, but in the actual operation found that must be coupled with the delay to ensure that the program can be completed normally. The details of the implementation are attached below with a simple time delay function. The code has been tested.   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 ();    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 considerations for entering into the second   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 can be done as follows: <script type= "Text/javascript" language= "JavaScript" >     & nbsp  window.opener.document.location.reload (); </script> <boey onload= "opener.location.reload ();" >   Windows Refresh <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.