how to remove session when browser closed.
ASP.NET
4
Posts
2
Posters
0
Views
1
Watching
-
-
-
try this: 1. First create a page LogOut.aspx and in Page_Load event write this code:-
protected void Page_Load(object sender, EventArgs e)
{
Session.Abandon();
}2. Then add following JavaScript code in your page or Master Page:-
<script type="text/javascript">
var clicked = false;
function CheckBrowser()
{
if (clicked == false)
{
//Browser closed
}
else
{
//redirected
clicked = false;
}
}function bodyUnload() { if (clicked == false)//browser is closed { var request = GetRequest(); request.open ("GET", "AutoLogOut.aspx", true); request.send(); } } function GetRequest() { var request = null; if (window.XMLHttpRequest) { //incase of IE7,FF, Opera and Safari browser request = new XMLHttpRequest(); } else { //for old browser like IE 6.x and IE 5.x request = new ActiveXObject('MSXML2.XMLHTTP.3.0'); } return request; }
**3. Add the following code in the body tag of master page.
<body onunload="bodyUnload();" Onclick="clicked=true;">
Finally the code in Master page like this:-
//<!\[CDATA\[ var clicked = false; function CheckBrowser() { if (clicked == false) { //Browser closed } else { //redirected clicked = false; } } function bodyUnload() { if (clicked == false)//browser is closed { //var request = GetRequest(); //location.href = 'LogOut.aspx'; var request = GetRequest(); request.open("GET", "LogOut.aspx", true); request.send(); } } function GetRequest() { var request = null; if (window.XMLHttpRequest) { //incase of IE7,FF, Opera and Safari browser request = new XMLHttpRequest(); } else { //for old browser like IE 6.x and IE 5.x request = new ActiveXObject('MSXML2.XMLHTTP.3.0'); } return request; }
</x-turndown>
**