Frustrating Firefox Problem while making AJAX request on an anchor

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Sep 2008
Posts: 5
Reputation: ximath is an unknown quantity at this point 
Solved Threads: 0
ximath ximath is offline Offline
Newbie Poster

Re: Frustrating Firefox Problem while making AJAX request on an anchor

 
0
  #11
Sep 4th, 2008
Originally Posted by Bob Holmes View Post
I'm not certain if this is what you are looking for. It works in both IE and FF. AJAX requests are made several times in quick succession (before the previous request can be completed) from the onload event of this page: http://mmpp.com/mcweb200.nsf/formYieldSearch?Openform. Section 2. and section 4. on the page show 'Loading' in the listboxes until the AJAX request has been completed. You can also click on section 1. Municipalities radiobutton before the others are loaded. I hope this helps.

I should also mention that it is essential that the request_via_http variable and the triggered onreadystatechange function must remain within this function, or it doesn't work.


Hi,
I have tried to change href of anchor to javascript:void(0) and in this case it works in both IE and FF even if I don't wait hover request to complete. No problem. But when I change anchor's href to something else, say, http://www.google.com, this is where the problem begins. Thank you anyway.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 381
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: Frustrating Firefox Problem while making AJAX request on an anchor

 
0
  #12
Sep 5th, 2008
@ximath

Apparently I really like working on this puzzle ... I thought of this early this morning but have been away from my computer until now.

This is likely the way it should be done -- do you see what is different?

test_script.php (this is the same)
  1. <?php
  2. if ( $_REQUEST ) {
  3. sleep(2);
  4. print $_REQUEST['task'].'::'.$_REQUEST['foo'];
  5. exit;
  6. }
  7. ?>

main.html (this is changed)
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. <script type="text/javascript" src="agent.js"></script>
  7. <script type="text/javascript">
  8.  
  9. window.onload = register_triggers;
  10.  
  11. function register_triggers () {
  12. // onmouseover triggers
  13. document.getElementsByTagName('html').item(0).onmouseover = function ( event ) {
  14. var action = ( typeof event == 'undefined' ) ? window.event : event;
  15. var target = ( typeof action.target == 'undefined' ) ? action.srcElement : action.target;
  16. if ( target.nodeName == '#text' ) { // Safari nodeType 3 work-around
  17. target = target.parentNode;
  18. }
  19. // custom handlers here ...
  20. if ( /hover-ajax/.test( target.parentNode.className ) ) {
  21. makeAjaxRequest(target,'hover');
  22. }
  23. };
  24. // onclick triggers
  25. document.getElementsByTagName('html').item(0).onclick = function ( event ) {
  26. var action = ( typeof event == 'undefined' ) ? window.event : event;
  27. var target = ( typeof action.target == 'undefined' ) ? action.srcElement : action.target;
  28. if ( target.nodeName == '#text' ) { // Safari nodeType 3 work-around
  29. target = target.parentNode;
  30. }
  31. // custom handlers here ...
  32. if ( /click-ajax/.test( target.parentNode.className ) ) {
  33. makeAjaxRequest(target,'click');
  34. // no return value or cancel default action !!!
  35. }
  36. };
  37. }
  38.  
  39. function makeAjaxRequest ( target, type ) {
  40. var url = target.href;
  41. var args = 'foo=hede';
  42. switch ( type ) {
  43. case 'click': args += '&task=click'; break;
  44. case 'hover': args += '&task=hover'; break;
  45. }
  46. var req = new Agent();
  47. req.set_action('test_script.php');
  48. req.set_method('POST');
  49. req.set_format('text');
  50. // make the request synchronous and everything works
  51. req.set_async(false);
  52. if ( resp = req.request(args) ) {
  53. // now we get the result as a return value of the synchronous request
  54. // and the link doesn't execute until we get the return value ...
  55. alert( resp );
  56. }
  57. }
  58. </script>
  59. </head>
  60. <body>
  61. <span class="hover-ajax"><a href='http://www.google.com'>Hover Here</a></span><br /><br />
  62. <span class="click-ajax"><a href='http://www.google.com'>Click Here</a></span>
  63. </body>
  64. </html>

Okay, I'm done now :-)
Google is the answer to all of your questions -- the trick is knowing what question to ask in your specific predicament.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Frustrating Firefox Problem while making AJAX request on an anchor

 
0
  #13
Sep 6th, 2008
> What I'm doing is, triggering two requests before page is redirected, which means their
> destination is not undefined. When request is triggered, the page actually exists.

Of course the destination exists and so does the source when making the request, but clicking on the link loads a new page and hence the context in which the request was made is destroyed. Remember, Javascript is a client side scripting language in this case and hence its lifetime is the lifetime of the client session.

> What's more; if I wait for the request in hover phase to be completed and then I click, it
> certainly works without any error.

Because enough time is given for the request to complete and to process the response when you hover on the link. This isn't the case when you *click* on it since the browser attempts to load a new context.

> Moreover, the code I've posted here works perfectly in IE as it is.

Because what FF is showing in the Error console is not an error at all. Think about it, if you plan on ignoring the return status of your Ajax request, should it even matter to any browser? Does the same code run fine in Opera? Is this so called error hampering your functionality.

> About your suggestions; the reason why I wouldn't do either of these solutions was I
> didn't want to interfere the current principles of the system.

What kind of a system mandates you to forcefully use Ajax when the problem can be very easily solved with redirecting to a PHP page and performing navigating to your page of choice? Also, there are a lot many ways in which your current approach can be modified to work in a more predictable manner. One would be to navigate to the destination page after the Ajax requests are complete.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC