Questions tagged [readystate]

a property of both Document and XMLHttpRequest objects

readyState is a property of both Document and XMLHttpRequest objects.

Document.readyState

In a Document object, readyState represents the object's loading state. It has three possible values:

  • loading
  • interactive
  • complete

Example

When the readyState value changes, a Document.onreadystatechange event is fired:

document.onreadystatechange = function () {
  if (document.readyState === 'loading') {
    console.log('the document is still loading');
  } else if (document.readyState === 'interactive') {
    console.log('the document has loaded, but sub-resources are still loading');
  } else if (document.readyState === 'complete') {
    console.log('the document and its sub-resources are loaded');
  }
}

XMLHttpRequest.readyState

In an XMLHttpRequest object, readyState represents the current state of the request's lifecycle. It has five possible values:

  • UNSENT (0)
  • OPENED (1)
  • HEADERS_RECEIVED (2)
  • LOADING (3)
  • DONE (4)

Example

When the readyState value changes, a XMLHttpRequest.onreadystatechange event is fired:

var request = new XMLHttpRequest();

request.onreadystatechange = function () {
  if (request.readyState === 1 || 
    request.readyState === XMLHttpRequest.OPENED) { 
    console.log('the request has been successfully opened');
  } else if (request.readyState === 2 || 
    request.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
    console.log('the response headers have been received');
  } else if (request.readyState === 3 || 
    request.readyState === XMLHttpRequest.LOADING) {
    console.log('the response body is being received');
  } else if (request.readyState === 4 || 
    request.readyState === XMLHttpRequest.DONE) {
    console.log('the request is complete');
  }
};

request.open('get','http://stackoverflow.com/');
request.send();

Resources

Related Tags

129 questions
4
votes
2 answers

How does readyState work?

I check the property .readyState of BrowserTestObject using RFT (Rational Functional Tester). Sometimes before the next step in my test script I need to be sure that the page is 'ready' and all objects are loaded. Does the .readyState == 4 mean…
Radek
  • 13,813
  • 52
  • 161
  • 255
4
votes
1 answer

Valums file uploader xhr returns status 0, readystate 4 and responseText empty

I am using valums fileuploader in asp.net web application. It's working fine with the actual uploads as such. But the error condition checking is not working properly in Chrome and FF. The uploader points to a handler called fileupload.ashx which…
4
votes
1 answer

Are there useful uses of readyState different from "4" (complete) in a XHR.onreadystatechange callback?

Have you ever used an XHR object to intercept onreadystatechange with a readyState different from "4" (complete) ? I'm curious to know if you ever trigger a function with the possible different values. I cannot imagine a real use of the others…
yuri
  • 575
  • 1
  • 14
  • 33
4
votes
1 answer

HTML

I have separate
ea7ababe
  • 373
  • 5
  • 13
4
votes
3 answers

Get ReadyState from WebBrowser control without DoEvents

This has been awnsered many times here and at other sites and its working, but I would like ideas to other ways to: get the ReadyState = Complete after using a navigate or post, without using DoEvents because of all of its cons. I would also note…
Combustion
  • 41
  • 1
  • 1
  • 4
4
votes
1 answer

When did Internet Explorer introduce readyState property on the document object?

Internet Explorer supports the readyState property on the document... http://msdn.microsoft.com/en-us/library/ie/ms534359(v=vs.85).aspx But which version of IE was this introduced in?
Andy Hume
  • 40,474
  • 10
  • 47
  • 58
3
votes
1 answer

AJAX / History - When is the right time to call the `pushState` method in AJAX app?

I'm working with AJAX and history object (with the .pushState method). I just want to know when is the right time to call pushState method? Is it before request? after request? or on what state of XMLHttpRequest's readyState should I call it? As…
kazinix
  • 28,987
  • 33
  • 107
  • 157
3
votes
1 answer

How is the onreadystatechange event supported everywhere but document.readyState isn't?

According to MDN here and here, it says that the readystatechange event is supported in all browsers, but the document.readyState property is only supported back until around IE9+ (8*). This doesn't really make sense, given that literal definition…
3
votes
3 answers

XMLHttpRequest onreadystatechange never fired for readyState 0

I set up the usual XMLHttpRequest and everything works fine so far, I can retrieve data and readyState changes and does all sorts of things. One thing that doesn't happen though is that readyState ever triggers the onreadystatechange() function when…
3
votes
1 answer

Different Behavior of XMLHttpRequest for vs.

Consider the following code: index.html
Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
3
votes
2 answers

Is it possible to change document.readyState with JavaScript?

I'm loading HTML via an iframe on a site. The HTML being loaded is supposed to wait to load certain content only after the parent document has finished loading (document.readyState == 'interactive' || document.readyState == 'complete'). The problem…
amdilley
  • 103
  • 1
  • 5
3
votes
7 answers

Run JavaScript after all window.onload scripts have completed?

I have an asp.net page that loads some JavaScript scripts. One of those scripts loads some controls into the page appending them to the body in the window.onload event. I need to inject a script via code behind to call a method of the scripts that…
Dante
  • 3,833
  • 4
  • 38
  • 55
3
votes
3 answers

Comet Jetty/Tomcat, having some browser issues with Firefox and Chrome

I am exploring the use of Comet for a project I am working on. I tried creating a test application first using Tomcat6 and CometProcessor API and then with Jetty7 Continuations. The application is kind of working on both but I am having some issues…
ages04
  • 6,337
  • 4
  • 19
  • 15
3
votes
2 answers

Javascript pop-up window document is never "ready"

I'm using Javascript to open a blank window, populate it with the bare minimum and inject a script tag to include JQuery, but the window property readyState never gets past loading, therefore JQuery never triggers. Here is the demo on jsFiddle. I…
scader
  • 405
  • 3
  • 8
  • 19
2
votes
2 answers

Problem with Application.DoEvents() while waiting for WebBrowser to finish loading

I'm trying to load WebBrowser content and after that I want to add some text and scroll to the bottom. Here's example of my code: webBrowser1.Url = new System.Uri("file:///" + filePath); webBrowser1.Document.Body.InnerHtml +=…
alcohol is evil
  • 686
  • 12
  • 34
1
2
3
8 9