By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,259 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,259 IT Pros & Developers. It's quick & easy.

Problems using AJAX and hyperlinks simultaneously on FF1.5.0.4

P: n/a
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>

Jul 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 22 Jul 2006 02:28:53 -0700, "Tom" <tf******@gmail.comwrote:
>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?
It's without question 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.
just target an IFRAME you create, if you're doing so much hacky stuff,
another one really doesn't matter.

Jim.
Jul 22 '06 #2

P: n/a
Tom

Jim Ley wrote:
On 22 Jul 2006 02:28:53 -0700, "Tom" <tf******@gmail.comwrote:
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?

It's without question 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.

just target an IFRAME you create, if you're doing so much hacky stuff,
another one really doesn't matter.

Jim.
OK, I can do this. I would rather not be hacky, but if its to work
around a bug then sure.

You say I am doing so much hacky stuff. Was it something in my code or
my explanation of what I was trying to do? Could you point out what you
meant?

If I am going about this the wrong way, then I would rather start again
and start a better way.

Cheers,

Tom...

Jul 22 '06 #3

P: n/a
On 22 Jul 2006 10:38:33 -0700, "Tom" <tf******@gmail.comwrote:
>You say I am doing so much hacky stuff. Was it something in my code or
my explanation of what I was trying to do? Could you point out what you
meant?
Sure, a registered protocol handler, seperate reporting with xmlhttp
request etc. - none of it is stuff that is normal web stuff, and
you'll need to test carefully with any client you wish to use.
>If I am going about this the wrong way, then I would rather start again
and start a better way.
Well we have no idea what you want to achieve :) I would've thought
having the 3rd party tool report it's being used - rather than the XHR
which only indicates the attempt would make some sense for example.

Jim.
Jul 22 '06 #4

P: n/a
Tom

Jim Ley wrote:
On 22 Jul 2006 10:38:33 -0700, "Tom" <tf******@gmail.comwrote:
You say I am doing so much hacky stuff. Was it something in my code or
my explanation of what I was trying to do? Could you point out what you
meant?

Sure, a registered protocol handler, seperate reporting with xmlhttp
request etc. - none of it is stuff that is normal web stuff, and
you'll need to test carefully with any client you wish to use.
If I am going about this the wrong way, then I would rather start again
and start a better way.

Well we have no idea what you want to achieve :) I would've thought
having the 3rd party tool report it's being used - rather than the XHR
which only indicates the attempt would make some sense for example.

Jim.
Fair points :) Thanks.

What I am trying to achieve: I am just trying to make a web front end
for playing my tv shows etc(Don't have access to any of them on normal
TV in the country I live in). I want to be able to launch a show (which
I am doing with mplayer) and update the server so it knows which shows
could have been watched.

Partially re-inventing the wheel, yes, but mostly just a self-exercise
for me in JS and stuff like that. I have a registered protocol as just
having a hyperlink to a media file always tries to download the file
locally and ask what to do with it etc etc( I realise this means
modifying the client which annoys me intensely, but I was a bit stuck
there...). Reporting back with XHR cos.. well, I would like to get more
to grips with it :)

I guess I will just continue along with my bodges for this little
project and go ahead and use the iframe (which works just great, ta!).
I'll just keep in mind that this is one of those bodgy projects :)

Cheers,

Tom...

Jul 23 '06 #5

P: n/a
On 23 Jul 2006 00:48:27 -0700, "Tom" <tf******@gmail.comwrote:
>I guess I will just continue along with my bodges for this little
project and go ahead and use the iframe (which works just great, ta!).
I'll just keep in mind that this is one of those bodgy projects :)
There's nothing wrong with such projects, it's a great way to learn
stuf, remember hacky is not a perjorative, it can be a hacky project
and still be good, and still be the best way of doing what you're
doing.

Jim.
Jul 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.