Forum Moderators: open
<a href="#" onclick="window.opener.ajaxFunction();self.close();return false;">close this window</a>"
This works fine in IE7, but Firefox starts the new ajax request but gets stuck at readystate == 2. For troubleshooting, I have a mouseover link on the "main" (window.opener) page that i can use to manually trigger my ajaxFunction() script, and that link successfully refreshes the content in Firefox, so the problem has something to do with calling it remotely and then closing out the window (i think). This is the link (in the main window) that works fine:
<a href="#" onmouseover="ajaxFunction();">RELOAD (mouseover)</a>
Here is my (very generic) AJAX scripting:
<!-- AJAX BEGIN -->
<script language=\"javascript\" type=\"text/javascript\">
<!--
//Browser Support Code
function ajaxFunction()
{
var ajaxRequest; // The variable that makes Ajax possible!
try
{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer Browsers
try
{
ajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");
}
catch (e)
{
try
{
ajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");
}
catch (e)
{
// Something went wrong
alert(\"Your browser broke!\");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function()
{
if(ajaxRequest.readyState == 4 ¦¦ ajaxRequest.readystate == 'complete')
{
if (ajaxRequest.status == 200)
{
// SUCCESS
// the next line inserts retrieved data into a DIV area called <AjaxContentDiv>
document.getElementById('AjaxContentDiv').innerHTML=ajaxRequest.responseText;
}
else
{
// this might be a 404 if the page is missing (or other error number)
document.getElementById('AjaxContentDiv').innerHTML = '<strong><font color=\"red\">ERROR: Unable to access source data.</font></strong>';
}
}
else
{
// readyState has changed but <> 4
document.getElementById('AjaxContentDiv').innerHTML = '<img src=\"images/ajax_loading.gif\"> LOADING...'+ajaxRequest.readyState;
}
}
var lookupRequest = 'ajax_display_options.php?OpdId=$OpdId';
ajaxRequest.open(\"GET\", lookupRequest, true);
ajaxRequest.send(null);
}
//-->
</script>
<!-- AJAX END -->
Any ideas why Firefox is unhappy and what I can do to resolve this? Thanks!
First of all, here is the line that is called in the popup that triggers the ajax reload in the main window and also closes the popup at the same time:
onLoad="window.opener.ajaxFunction();self.close();return false;"
Here is why that matters...it all works (still) in Internet Explorer 7. It still does not work in Firefox. I have discovered that the problem is that the popup closes before the ajax call function completes. If I remove the self.close() command from my popup window, the main window refreshes its content with no problem. Unfortunately, I need to have the popup close itself...so this is not a solution. Is there a way to tell the ajaxFunction() to execute and not care if the code that initially told it to execute sticks around? I know i could build in a delay on the self.close() but that is not really the solution i am looking for either.
Hopefully someone knows what I can do to solve this for Firefox. THANKS!
I don't know if this will force js to run in the opener window.
Playing with setTimeout sometimes fixes problems:
onLoad="window.opener.ajaxFunction();setTimeout(function(){self.close()},0);return false;"
Tell me if one of them works.