Search code examples
asp.netajaxserver-tags

Why is my onbeforeunload not running now when it was before? Or AJAX failing on second identical command?


This follows on from this question

This was working:

<body onbeforeunload=
 "ajaxRequest('UnlockQuery.ashx?QueryID=266&UserID=11631');"> 

This was created using the following in the aspx page:

<body onbeforeunload=
 "ajaxRequest('UnlockQuery.ashx?QueryID=<%= Session["QueryId"] %>&
 UserID=<%= Session["UserID"] %>')">

This is not working:

<body id="uxBodyTag" onbeforeunload=
 "ajaxRequest('UnlockQuery.ashx?QueryID=266&amp;UserID=11631');"> 

This is created using:

uxBodyTag.Attributes["onbeforeunload"] += 
 "ajaxRequest('UnlockQuery.ashx?QueryID=" + 
 queryId.ToString() + "&UserID=" + Session["UserID"].ToString() + "');";

The code being called is this:

function ajaxRequest(url)
{
    xmlhttp=null;
    if (window.XMLHttpRequest)
    {   // code for all new browsers
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {   // code for IE5 and IE6
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp!=null)
    {
        xmlhttp.onreadystatechange=null;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
    }
}

EDIT:

It appears to only fail when subsequent calls are made to the same unlock. I think this may be an AJAX issue....


Solution

  • Adding

    &date=DateTime.now.Ticks.ToString()
    

    seems to have fixed it. I don't think IE7 likes it when the same AJAX call comes in and the previous hasn't been "resolved" (the page is disposed before the AJAX call returns).

    Thanks to all that provided help.