T
Tom
Hi,
I am working on a project where I want to simultaneously launch a link
using my own registered protocol and simultaneously send an AJAX
request to the server.
Now the way I have come up with doing this is by having a normal link
(so that the link still works still if JavaScript is disabled) and an
onClick handler which makes the XMLHTTP request.
The problem is that the XMLHTTP responseText is always null when I do
this. If I execute the same code but from a non-link tag then the
server response comes back just fine (be it the response text or a 404
or whatever).
Something is happening differently when a the XMLHTTP request is sent
from a link. It works ok in I.E. which does not really concern me as I
am turning away from there as my primary browser.
Now I have seen some other people have a similar problem but I have not
seen any solution that I can follow. So am asking outright here using
my own example.
Now I have seen some people that say this is the correct behaviour as,
even though you are not actually navigating away from the page with the
link, a link still implies that this is your intent and so it does
garbage disposal on all the objects. Is this the case or is it a bug?
If it is correct behaviour then I need to work on my solution somewhat
Any ideas are welcome. I would rather start from scratch with a more
correct method than do annoying bodges like specifying a new target
window and then having to clean up the blank windows after.
I guess one way is to do the AJAX, process the response and then launch
the app using my registered url protocol. But this would mean waiting
for the server response before launching the app. I would prefer to
launch the app first and then let the server do what it needs to do
when the web page is in the background.
I have tried returnign false from the onClick handler, but it still
follows the link...
Any ideas? Comments much appreciated. Code follows.
Thanks,
Tom....
Test code. This code is just the bare minimum from my project so that
you can see the problem in effect. it may not be 100% gramatically
correct, but it is correct enough do demonstrate my problem. Click on
"test" and the AJAX tries to exacute (just showing you the 404 in the
response). Click on the link and the response is null...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;
charset=windows-1250">
<title>Test</title>
<script type="text/javascript"><!--
var http;
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
function updateserver(showId){
http.open('get', 'backend.php ');
http.onreadystatechange = handleServerResponse;
http.send(null);
}
function handleServerResponse() {
if(http.readyState == 4){
var response = http.responseText;
alert(response);
}
}
function initialise(){
http = createRequestObject();
}
// -->
</script>
</head>
<body onload="initialise()">
<a href="myprotocol:c:\test.bin" onClick=updateserver(1)>Link</a>
<br><b onClick=updateserver(1)>Test</b>
</body>
</html>
I am working on a project where I want to simultaneously launch a link
using my own registered protocol and simultaneously send an AJAX
request to the server.
Now the way I have come up with doing this is by having a normal link
(so that the link still works still if JavaScript is disabled) and an
onClick handler which makes the XMLHTTP request.
The problem is that the XMLHTTP responseText is always null when I do
this. If I execute the same code but from a non-link tag then the
server response comes back just fine (be it the response text or a 404
or whatever).
Something is happening differently when a the XMLHTTP request is sent
from a link. It works ok in I.E. which does not really concern me as I
am turning away from there as my primary browser.
Now I have seen some other people have a similar problem but I have not
seen any solution that I can follow. So am asking outright here using
my own example.
Now I have seen some people that say this is the correct behaviour as,
even though you are not actually navigating away from the page with the
link, a link still implies that this is your intent and so it does
garbage disposal on all the objects. Is this the case or is it a bug?
If it is correct behaviour then I need to work on my solution somewhat
Any ideas are welcome. I would rather start from scratch with a more
correct method than do annoying bodges like specifying a new target
window and then having to clean up the blank windows after.
I guess one way is to do the AJAX, process the response and then launch
the app using my registered url protocol. But this would mean waiting
for the server response before launching the app. I would prefer to
launch the app first and then let the server do what it needs to do
when the web page is in the background.
I have tried returnign false from the onClick handler, but it still
follows the link...
Any ideas? Comments much appreciated. Code follows.
Thanks,
Tom....
Test code. This code is just the bare minimum from my project so that
you can see the problem in effect. it may not be 100% gramatically
correct, but it is correct enough do demonstrate my problem. Click on
"test" and the AJAX tries to exacute (just showing you the 404 in the
response). Click on the link and the response is null...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;
charset=windows-1250">
<title>Test</title>
<script type="text/javascript"><!--
var http;
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
function updateserver(showId){
http.open('get', 'backend.php ');
http.onreadystatechange = handleServerResponse;
http.send(null);
}
function handleServerResponse() {
if(http.readyState == 4){
var response = http.responseText;
alert(response);
}
}
function initialise(){
http = createRequestObject();
}
// -->
</script>
</head>
<body onload="initialise()">
<a href="myprotocol:c:\test.bin" onClick=updateserver(1)>Link</a>
<br><b onClick=updateserver(1)>Test</b>
</body>
</html>