我想把DecreaseSession()执行完,再关闭浏览器,但是由于DecreaseSession()里用到了异步调用方法
Test.setSession(),所以当我X掉浏览器窗口时,onbeforeunload 先把浏览器关闭了,而DecreaseSession()并没有执行。加上event.returnValue=""后固然可以让异步调用完成,但是每次弹出框很不爽。请大家给支支招吧,怎么能不弹出框又可以等异步调用完了后再关闭Ie?代码如下:window.onbeforeunload = function()
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey)
{ var x= DecreaseSession();
if (x=="data"){ event.returnValue="";//加了这句可以确保DecreaseSession();执行完成。
//但是不想加,因为弹出的框很烦人。
}else
{
alert("未收到值!");
} }
} function DecreaseSession(){ var temp = <%=session.getAttribute("windowsCounterht")%>; temp = temp-1; var name = "windowsCounterht"; Test.setSession(temp,name,function callback(data){
window.opener.location.href=data;
window.close();});
return "data";
}
Test.setSession(),所以当我X掉浏览器窗口时,onbeforeunload 先把浏览器关闭了,而DecreaseSession()并没有执行。加上event.returnValue=""后固然可以让异步调用完成,但是每次弹出框很不爽。请大家给支支招吧,怎么能不弹出框又可以等异步调用完了后再关闭Ie?代码如下:window.onbeforeunload = function()
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey)
{ var x= DecreaseSession();
if (x=="data"){ event.returnValue="";//加了这句可以确保DecreaseSession();执行完成。
//但是不想加,因为弹出的框很烦人。
}else
{
alert("未收到值!");
} }
} function DecreaseSession(){ var temp = <%=session.getAttribute("windowsCounterht")%>; temp = temp-1; var name = "windowsCounterht"; Test.setSession(temp,name,function callback(data){
window.opener.location.href=data;
window.close();});
return "data";
}
比如value=""试试
function DecreaseSession(){ var temp = <%=session.getAttribute("windowsCounterht")%>; temp = temp-1; var name = "windowsCounterht"; Test.setSession(temp,name,function callback(data){
window.opener.location.href=data;
//加入如下2行代码
window.operer = null;
open('', '_self');
window.close();});
return "data";
}
var x= DecreaseSession();
if (x=="data"){
}
//这种判断返回值的方法存在问题,由于DWR是异步,有时执行 DecreaseSession()函数,不会立即返回x
后面的那一条你说的很对,这个我也知道,是为了发贴才这么写的。其实原来我的代码是
window.onbeforeunload = function()
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey)
{ DecreaseSession();
}
}
直接用的 DecreaseSession(),但由于是异步,窗口关闭后导致DecreaseSession()不能执行完成。