
代碼
//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;
}
}