Hi All,
I have been desperately trying to solve a serious ajax issue and decided to try asking it here.
I have simplified the problem and will illustrate it with an example.
First of all, please consider;
<span onClick='makeClickRequest();'><a href='http://www.google.com'>Text Here</a></span>
where the javascript is about
function makeClickRequest()
{
var req = new AsyncRequest();
req.setUrl("requestPage.php?task=click");
req.setVariables("foo=hede");
req.setMethod("POST");
req.setSuccessCallback( function(o) { alert(o.responseText); } );
req.makeRequest();
}
PS : You don't need to worry about AsyncRequest type, this simply directs AJAX requests by encapsulating an instance of XMLHTTPRequest type. I have used AsyncRequest class widely (and safely) in many applications and I am pretty sure that it works fine, so I don't think that it is necessary to put AsyncRequest's code here.
Now, if you open the page and click there, (assuming requestPage.php exists at the server)
in firefox you'll get the exception :
[Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"
Although nothing seems wrong with the code above, it wasn't working. I have tried to modify makeClickRequest as;
function makeClickRequest()
{
var callback = function()
{
var req = new AsyncRequest();
req.setUrl("requestPage.php?task=click");
req.setVariables("foo=hede");
req.setMethod("POST");
req.setSuccessCallback( function(o) { alert(o.responseText); } );
req.makeRequest();
}
window.setTimeout(callback, 0);
}
And it worked ! (strange, eh? )
However, I also need to make a request when the user hovers the anchor. So, consider that I modify the HTML (of anchor) at top of the page as;
<span onClick='makeClickRequest();' onMouseOver='makeHoverRequest();'><a href='http://www.google.com'>Text Here</a></span>
Where makeHoverRequest is
function makeHoverRequest()
{
var req = new AsyncRequest();
req.setSuccessCallback( function(o) { } );
req.setUrl("requestPage.php?task=hover");
req.setVariables("foo=hede");
req.setMethod("POST");
req.makeRequest();
}
Again, nothing seems wrong with that. However, if user clicks the link before request in mouseOver phase is completed, firefox throws an exception;
Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"
The only solution I was able to come up with was changing HTML as;
<span onClick='makeClickRequest(); var instance = this; var callback = function() { location.replace(instance.firstChild); } window.setTimeout(callback, 3000); return false;' onMouseOver='makeHoverRequest();'><a href='http://www.google.com'>Text Here</a></span>
Which is ugly, and waits 3 seconds to open link.. I guess I'm stuck.
Thanks in advance.