Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:10:55+00:00 2026-06-14T23:10:55+00:00

The W3C spec suggest following implementation: Some simple code to do something with data

  • 0

The W3C spec suggest following implementation:
Some simple code to do something with data from an XML document fetched over the network:

function processData(data) {
  // taking care of data
}

function handler() {
  if(this.readyState == this.DONE) {
    if(this.status == 200 &&
       this.responseXML != null &&
       this.responseXML.getElementById('test').textContent) {
      // success!
      processData(this.responseXML.getElementById('test').textContent);
      return;
    }
    // something went wrong
    processData(null);
  }
}

var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "unicorn.xml");
client.send();

Is this implementation really correct?

During debug I found cases when the readystatechanged event handler is called more than once in a row with the same value of readyState == 4. I guess this behavior is correct, as specs says that each change of state must fire the event, and that readyState must always be equal to current state, so if several events pile up in the events queue, it’s quite obvious, that one will get multiple calls with readyState == 4.

http://jsfiddle.net/44b3P/ — this is the above example augmented with debugger call to pause execution just after the request is send, and with alert() in the processData. Once you unpause the execution you will get 3 alerts.

This example from w3c seems to be copy&pasted in multiple places in the web — in particular OpenSocial seems to handle xhr events this way. Is it correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:10:56+00:00Added an answer on June 14, 2026 at 11:10 pm

    I also saw the same behavior (DONE 200 being hit more than once, i.e. 2…n times) when debugging in Chrome Developer Tools.

    To make it work always in debug mode as well, I found two options:

    1. Use onload to verify the success of an XMLHTMLRequest W3C Spec

      client.onload = handler;

      This version will not work in IE8. It must be a browser that implements the XMLHttpRequestEventTarget interface

    2. Remove the onreadystatechange handler as soon as you have a DONE 200 state.

      client.onreadystatechange = function() {
          // check if request is complete
          if (this.readyState == this.DONE) {
              if (this.onreadystatechange) {
                  client.onreadystatechange = null;
                  handler();
              }
          }
      };
      

    I have also opened an issue in Chromium for this problem, providing your Fiddle (Thanks!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a W3C Document object using a String value. Once I created
Is there a W3C document type available with both XHTML 1.0 transitional support and
The following w3c documents mentions rowgroup http://www.w3.org/TR/html401/struct/tables.html#h-11.2.6 http://www.w3.org/TR/html401/struct/tables.html#rowgroups <!ENTITY % Scope (row|col|rowgroup|colgroup)> Is there
Code is W3C valid, and everything should be fine. But when user clicks the
Why W3C Validator showing parsing error on this. I selected CSS3 profile also from
The w3c validator service complains that the following html is invalid. It does not
I've seen several posts here on the subject, and I've read the W3C spec
The d3js doc says I can use the W3C selectors spec to select objects
I have code something like this: <a href=http://google.com target=_blank>google</a> <a href=http://gmail.com target=_blank>gmail</a> Whenever I
I use the Java (6) XML-Api to apply a xslt transformation on a html-document

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.