问题描述
我希望在关闭浏览器或asp.net中的标签时过期会话。我尝试过以下不符合我要求的解决方案。问题是即使从一个页面导航到另一个页面或刷新页面时也会调用卸载方法。因此,每次用户导航到另一个页面时,它都会使会话无效。
我尝试了什么:
I want to expire session on closing browser or tab in asp.net. I have tried following solutions which are not fulfill my requirement.Problem is that the unload method is called even when you navigate from one page to the other or when you refresh the page. So it invalidates the session every time the user navigates to another page.
What I have tried:
<big>Solution 1:</big>
<body onunload="bodyUnload();" Onclick="clicked=true;">
<script type="text/javascript">
var clicked = false;
function bodyUnload()
{
if (clicked == false)//browser is closed
{
var request = GetRequest();
request.open ("POST", "../LogOut.aspx", false);
request.send();
}
}
function GetRequest()
{
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
</script>
<big>Solution 2 :</big>
函数ajaxFunction()
{
var xmlHttp;
尝试
{
// Firefox,Opera 8.0 +,Safari
xmlHttp = new XMLHttpRequest();
}
catch( e)
{
// Internet Explorer
试试
{
xmlHttp = new ActiveXObject(Msxml2.XMLHTTP);
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject(Microsoft.XMLHTTP);
}
catch(e)
{
alert(您的浏览器不支持AJAX!);
返回false;
}
}
}
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
document.myForm.time.value = xmlHttp.resp onseText;
}
}
xmlHttp.open(GET,customer_logout.php,true);
xmlHttp.send(null);
}
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","customer_logout.php",true);
xmlHttp.send(null);
}
推荐答案
这篇关于关闭ASP.NET中关闭浏览器或选项卡的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!