dcsimg
AJAX crashes if user closes window before response.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Simon_Baker
Posted On:   Monday, September 18, 2006 08:14 PM

We have a commercial web-based application with users using IE6 or later. Our server-side platform is Apache Tomcat for JSP support. We need to clean up some session objects when a user closes one of the browser windows in a multi-browser session, or moves to another page in a single browser session. We do this with the body onUnload event of the page. Currently, in the onUnload() handler, we open a new small window (javascript window.open()) targeting a JSP that cleans up the session objects while the closing window closes. The new small window closes itself with JavaScript as soon as it is displayed. It works, but the small window flashes on the screen, not very slick. We thought maybe AJAX could    More>>

We have a commercial web-based application with users using IE6 or later. Our server-side platform is Apache Tomcat for JSP support.

We need to clean up some session objects when a user closes one of the browser windows in a multi-browser session, or moves to another page in a single browser session. We do this with the body onUnload event of the page. Currently, in the onUnload() handler, we open a new small window (javascript window.open()) targeting a JSP that cleans up the session objects while the closing window closes. The new small window closes itself with JavaScript as soon as it is displayed.

It works, but the small window flashes on the screen, not very slick.

We thought maybe AJAX could be used to submit the request to the JSP that does the cleanup, avoiding the new window. It works, sort of. The target JSP does get the request, and does clean up the resources. But the browser gets hung in the process. We were hoping that if the Window waiting for the AJAX response continued the closing process, no harm would be done. Unfortunately, it hoses the browser. Typically, we can use the method twice before the browser dies.

I'm wondering if anyone else has encountered this type of problem, and if there is a solution.

The essential code is included below.

--------------------------------------------------------

			
var gIsUserAbandoningWindow = true;
var xmlhttp;
//----------------------------
function loadXMLDoc(url)
{
xmlhttp=null
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("[jsp/report_custom_detail_view_job_ran.jsp] Your browser does not support XMLHTTP."
+ " You may keep working, but XMLHTTP browser support is needed for automatic deletion"
+ " of report jobs. You will need to delete the reports from the job monitor manually"
+ " by checkmarking the "Cancel" checkbox. Please "
+ "notify your administrator.");
}
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
// ...some code here...
}
else
{
alert("Problem retrieving XML data")
}
}
}

function bodyUnload()
{
if (gIsUserAbandoningWindow
)
{
var frm = document.forms[0];
var strWinName = "job_cancel";

if (frm)
{
var url = "job_cancel_x_ran.jsp?jobthreadid=" + frm.jobthreadid.value;
loadXMLDoc(url);
return;
}
}

   <<Less

Re: AJAX crashes if user closes window before response.

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, October 9, 2006 02:28 PM

have you tried just opening in a hidden iframe, instead of in a new separate tiny window?
About | Sitemap | Contact