SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cannot query XMLHttpRequest properties when async == false (FF 3.0.1)

    This appears to be a bug in Firefox 3.0.1 since it works fine in IE 7, but I thought I'd run it past you guys just in case there's a problem with my script. Note that the code is typical of other AJAX scripts I've written, which all work fine. The only difference is, the third argument to XMLHttpRequest.send is false instead of true. The reason for this is that I need to return the correct index into an array where the responseText of several requests will be stored. Firebug shows that the responseText is as expected, status == 200, and readyState == 4, but I cannot access these properties in the onreadystatechange handler. I can access them when async == true, but the array index returned would be unpredictable.

    TemplateManager.js
    Code:
    TemplateManager = {
    
     templates:             new Array(),
     xhr:                   ajax(),
    
     loadTemplate:          function(uri) {
                             with (TemplateManager) {
                              xhr.open('GET',uri,false);
                              xhr.onreadystatechange = TemplateManager.handleXhrResponse;
                              xhr.send('');
                              return templates.length - 1;
                             }
                            },
    
     getTemplate:           function(handle) {
                             with (TemplateManager) {
                              return templates[handle];
                             }
                            },
                            
     handleXhrResponse:     function() {
                             with (TemplateManager) {
                              if (xhr.readyState == 4) {
                               if (xhr.status == 200) templates.push(xhr.responseText);
                               else alert('HTTP error ' + xhr.status + ' loading template.');
                              }
                             }
                            }
                             
    };
    template.jst
    Code:
    Hello ${customer.first} ${customer.last}.<br/>
    Your shopping cart has ${products.length} item(s):
    <table>
     <tr><td>Name</td><td>Description</td>
         <td>Price</td><td>Quantity & Alert</td></tr>
     {for p in products}
         <tr><td>${p.name|capitalize}</td><td>${p.desc}</td>
             <td>$${p.price}</td><td>${p.quantity} : ${p.alert|default:""|capitalize}</td>
             </tr>
     {forelse}
         <tr><td colspan="4">No products in your cart.</tr>
     {/for}
    </table>
    {if customer.level == "gold"}
      We love you!  Please check out our Gold Customer specials!
    {else}
      Become a Gold Customer by buying more stuff here.
    {/if}
    template_test.html
    Code:
    <html><head>
    <script src="client.js"></script>
    <script src="TemplateManager.js"></script>
    <script>
    var tH = TemplateManager.loadTemplate('/template.jst');
    var t = TemplateManager.getTemplate(tH);
    alert(t);
    </script>
    </head><body></body></html>
    The alert call shows "undefined".

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another thing I should mention:

    I placed several alerts inside TemplateManager::handleXhrResponse to see what was going on. None of these alerts were triggered, which seems to indicate the handler isn't even being called.

  3. #3
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Twenty-six views, no replies. Am I to assume the issue is with Firefox 3.0.1 and not my code then?

  4. #4
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As usual, I fixed the problem myself while waiting all day for a response. Sorry to have troubled you.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Can you spare the puzzled members out there and let us know how you fixed it?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Can you spare the puzzled members out there and let us know how you fixed it?
    Google.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    That's not very helpful. The purpose of these forums is to provide easily accessible answers to people problems, whether they be now or in the future.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by World Wide Weird View Post
    Google.
    mmj

  9. #9
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    That's not very helpful. The purpose of these forums is to provide easily accessible answers to people problems, whether they be now or in the future.
    That's correct. Yet, although I've answered dozens of questions on these forums, I've never gotten a quick, easy answer to a single one of my own tough questions here. Not one. In light of that, I should have known to google it in the first place instead of posting it here.

    Okay, the troll is over. Here's the answer. Firefox's XMLHttpRequest object doesn't fire the onreadystatechange event for synchronous requests. You have to use onload instead. This isn't the way the other browsers work, and it's not documented (as far as I know). Yet there is no standard enforcing a "correct" behavior, so one can't positively say that one way is right and the other is wrong. C'est la vie.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •