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
114
votes
5 answers

What do the different readystates in XMLHttpRequest mean, and how can I use them?

XMLHttpRequest has 5 readyStates, and I only use 1 of them (the last one, 4). What are the others for, and what practical applications can I use them in?
Marius
  • 57,995
  • 32
  • 132
  • 151
78
votes
6 answers

How to detect if DOMContentLoaded was fired

I'm trying to help developing a library and for it I'm trying to work with page loading. In the process I want to make the library completely compatible with the use of defer and async. What I want is simple: How can I know that DOMContentLoaded was…
brunoais
  • 6,258
  • 8
  • 39
  • 59
14
votes
5 answers

Ajax won't get past readyState 1, why?

I'm trying to get this function to work, which does a request for parameter url then sends the responseText to callback which is a function. It seems that it only gets to readyState 1 (thanks to the Firebug commands). Here it is: function…
kennyisaheadbanger
  • 488
  • 1
  • 4
  • 19
11
votes
6 answers

Method POST, Status (canceled) error message

I have the following code which is giving me a Method POST, Status (canceled) error message: $(document).ready(function() { var xhr = false; get_default(); $('#txt1').keyup( function() { if(xhr && xhr.readyState != 4){ …
oshirowanen
  • 15,297
  • 82
  • 198
  • 350
11
votes
3 answers

dynamically creating script: readyState never "complete"

I'm trying to do something AFTER a script is completely loaded. (IE8) Script I use for testing: http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js and the invalid one:…
pepsi600
  • 145
  • 1
  • 2
  • 7
9
votes
3 answers

jQuery $.ajax and readyStates

How to call the Ajax ready states on the jQuery $.ajax method?
tetris
  • 4,302
  • 6
  • 28
  • 41
9
votes
6 answers

Failproof Wait for IE to load

Is there a foolproof way for the script to wait till the Internet explorer is completely loaded? Both oIE.Busy and / or oIE.ReadyState are not working the way they should: Set oIE = CreateObject("InternetExplorer.application") oIE.Visible =…
user3408723
  • 145
  • 1
  • 2
  • 9
7
votes
0 answers

Deploy R shiny app on LAN: Uncaught TypeError: Cannot read property 'readyState' of null

I've built a shiny web app that I would like to share with my colleagues. I use for that runApp(host = getOption("shiny.host", "10.161.112.8")) where 10.161.112.8 is my host server IP adress. The app works perfectly on my local server, but some…
JeanBertin
  • 633
  • 1
  • 7
  • 23
7
votes
1 answer

Jquery S.Ajax error handler being executed if readystate=4 and status=200

I'm doing an $.ajax call, which is returning a json response and all seems fine, but instead of the success handler being invoked, the $.ajax error handler is invoked even though the readystate=4 and the status=200. The $.ajax call is:- …
epx
  • 87
  • 1
  • 1
  • 8
6
votes
1 answer

What does ‘readystatechange’ event for ‘window’ mean?

Trying to handle window.onreadystatechange, I notice this event firing two times during page load. But I cannot figure out what exactly becomes changed after each event. If it were not for window, but for document, then there had been…
user6423602
6
votes
1 answer

Workarounds for jsdom document.readyState being readOnly?

I'm using mocha with jsdom for unit testing of a JavaScript library. One of the modules in the library has different behavior depending on whether or not the document is ready. In order to test that behavior, I need to simulate a document that isn't…
Stephen Thomas
  • 13,843
  • 2
  • 32
  • 53
6
votes
3 answers

html5 video wait for readystate == 4 after setting currentTime

I'm trying to grap some images of a video that is not played. Therefore I use the HTML5 . Because I want to grab images that haven't been played, I set video.currentTime = y; If I now call the grap function it won't work. Because the…
fsulser
  • 853
  • 2
  • 10
  • 34
6
votes
1 answer

request.xhr undefined in Ext JS

my web site is made using Ext JS 4.1 framework and ASP .Net MVC v3. When new frame is rendered there are 19 separate AJAX requests for retrieving data in JSON-format. All requests are familiar and made by Ext.Ajax.request().…
Volodymyr Ivanov
  • 159
  • 2
  • 12
5
votes
1 answer

How to properly use .readyState in selenium-python to wait for a web page to be "complete"?

I'm trying to get selenium to wait for a page to fully load using .readyState but I can't find the correct way to get selenium to test the .readyState of the web page before proceeding. The two hashed out lines are my best attempts to get it to work…
zao
  • 83
  • 1
  • 1
  • 6
5
votes
4 answers

in jquery how to call a function after an ajax request has been sent?

How can I call my function: myFunc1 in jquery after the browser has completely sent an ajax request? I want to run a callback function after a jquery ajax has been sent. I don't want to do this on complete or success because I don't want to wait for…
user566245
  • 4,011
  • 1
  • 30
  • 36
1
2 3
8 9