DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 關閉ie窗口清除Session的解決方法
關閉ie窗口清除Session的解決方法
編輯:關於JavaScript     

復制代碼 代碼如下:
//function window.onunload() { alert('這就是你要做的事,關閉網頁之後做的! '); location = 'SessionClear.aspx'; }
        //function window.onbeforeunload() { alert('這就是你要做的事,關閉網頁之前做的! ') }

        function window.onunload() {

            if ((window.screenLeft >= 10000 && window.screenTop >= 10000) || event.altKey) {
                alert(window.screenLeft+","+window.screenTop);
                //用戶非正常關閉時需要觸發的動作
                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;
            }
        }
    }
}

解釋:

一般的會員形式的網站,在會員登陸後都會建立會話或者Cookie,然後需要在會員退出時點退出連接或按紐退出。在會員直接關閉窗體的時候,沒有觸發涉及到退出的一系列退出。而這些要等到服務器會話過期才會被清除掉。

有幸的是在網絡上終於找到可以捕獲用戶使用Alt+F4、標題欄按右鍵關閉、雙擊標題欄、直接按關閉按鈕的事件的方法。當然對於最小化到任務欄再進行關閉是不能捕獲的。
復制代碼 代碼如下:
<script language="javascript">
<!--
function window.onunload(){
if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey)
{
//用戶非正常關閉時需要觸發的動作
}
}
-->
</script>

說明:
window.screenLeft = 10000 + 邊框寬 (2×2) = 10004
window.screenTop = 10000 + 工具欄高 + 標題欄高 = 10097

需要說明的,在onBeforeUnload中屏幕這些坐標屬性都是正常數值。

screenLeft:獲取浏覽器客戶區左上角相對於屏幕左上角的 x 坐標。

screenTop:獲取浏覽器客戶區左上角相對於屏幕左上角的 y 坐標。

我猜測,在窗體銷毀時捕獲的將產生一個特殊的數值。在正常情況下的點擊測試,數值都不會超過該數值。

現在問題是,在onBeforeUnload中使用window.location可以正常地提交請求到指定URL,但在onUnload事件中該方法不能有效執行。解決的方法是新開一個窗口,在將之關閉掉。

我們可以寫上這樣的話,來代替以往使用的,一系列的window.location。因為門戶網站涉及到多個跨服務器的網站服務器。在統一入口退出後,需要依次地進行順序退出,才能達到門戶網站期望的效果。

var newWindow;
window.opener=null;
newWindow=window.open(URL,PageName, 'height=0,width=0');
newWindow.opener=null;
newWindow.close();
……

該段代碼已經經過測試,在onUnload裡不要使用window.close,因為對象銷毀前會立即觸發該事件。而onBeforeUnload是頁面將要被卸載前觸發的事件。

而所謂的清除實質上就是把做好退出功能的頁面,直接以打開新窗口方式的調用。可能在調用到關閉的時候能停頓一兩秒,或者關閉窗口放在專門的退出頁面。該頁面與正常退出又切回到首頁,不同的是退出後會自動進行關閉,不需要再另外進行控制直接打開即可。

[注意]在window.onUnload中如果不加判斷,則會在刷新頁面等導致當前頁面變化都會觸發該事件。因此必須要進行判斷,捕捉特定的操作,才能屏蔽掉一些正常的操作。

續:在IE中如何在用戶直接關閉窗口前清除Session

昨天曾經講解了實現思路,但在實際操作中發現,必須要加上延遲才能確保程序可以正常執行完畢。下面附上實現的細節,並附上簡單的時間延遲函數。代碼已經經過測試。
復制代碼 代碼如下:
<script language="javascript">
function window.onUnload()
{
var newWindow;
if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey)
{
newWindow=window.open(‘退出程序地址','網頁名稱',
'width=0,height=0,top=4000,left=4000');//新窗口將在視區之外打開
newWindow.opener=null;
sleep(5000);
newWindow.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; //考慮進時進分進秒的特殊情況
gaps=hourGaps*3600+ minuteGaps*60+ secondGaps;
gaps=gaps*1000+msGaps;
}while(gaps<milisecond);
}
</script>

二,在窗口加載或退出時,想讓浏覽器刷新一次可以如下操作:
<script type="text/javascript" language="javascript">
       window.opener.document.location.reload();
</script>
<boey onload="opener.location.reload();">   開窗時刷新
<body onUnload="opener.location.reload();">關閉時刷新

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved