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
1
vote
1 answer

Fetching data from server using XMLHttpRequest (for Phonegapp BlackBerry App)

I am developing a BlackBerry Phonegap application. Therefore, I am using JavaScript. From a local file, I am trying to fetch some data (bare text) located in the server. I am trying in different browsers (Mozilla and Chrome mainly). The code I am…
Arturo Moncada-Torres
  • 1,236
  • 14
  • 23
1
vote
1 answer

Why is my AJAX-call failing in Google Chrome?

I am curious as to why my AJAX-call is failing in Google Chrome, it works perfectly fine in Firefox. Before anyone asks, no I'm not using JQuery because I need to have access to readyState == 3 which JQuery doesn't seem to have. My script currently…
Tanax
  • 433
  • 2
  • 10
  • 20
1
vote
2 answers

Problem with XMLHttpRequest conditional XMLHttp.status==200

My Ajax works just fine until I add a conditional status property. Here's the snippet if (XMLHttp.readyState==4 && XMLHttp.status==200){ // do something } and here's the complete code function getXMLHttp() { try …
sin
  • 11
  • 1
  • 2
1
vote
1 answer

Running javascript when the DOM is modifyable/interactive

we have javascript code that injects elements into the DOM. Therefore it needs to wait until the DOM is in the required state. We use the following for this: document.onreadystatechange = function () { if (document.readyState === "interactive")…
user2693017
  • 1,750
  • 6
  • 24
  • 50
1
vote
1 answer

AJAX Results in Div Dissapearing

Using AJAX to call a CGI script which opens a file and prints it out in a DIV. This works fine on a test page I have but for some reason in another page it will print the contents into the DIV and then will clear out the DIV. Using alert() I can…
user770395
  • 33
  • 6
1
vote
1 answer

FileReader.readystate stuck at 1. Onload is not fired

I am quite newbie in angular7 and I just trying to get data from an input type file in the html file. I am not getting it. I got the name and different specifications of the file, but not the proper data of it. The point is readystate doesnt change…
Kenzo_Gilead
  • 2,187
  • 9
  • 35
  • 60
1
vote
1 answer

goto(url, {waitFor: 'load'}) function in puppeteer never resolves even though page.readyState === 'complete'

Using puppeteer: 1.3.0 Puppeteer navigates to the page using goto(url, {timeout: 0, waitUntil: 'load'}). It then never resolved simply leaving me on the page. I can open the console in the dev tools and type in document.readyState and get…
Niklas
  • 31
  • 4
1
vote
1 answer

Invoke readystatechange event in enzyme

I have a react class, which has the addEventListener in componentDidMount as below: componentDidMount() { document.addEventListener('readystatechange', () => { if (document.readyState === 'complete'){ /* Make some api call */ /*Need to test…
Shireesha
  • 11
  • 1
  • 3
1
vote
0 answers

How to detect the 'complete' event on a page loaded thanks to ajax requests?

I'm loading several div with innerHTML in a main page using ajax (xmlhtthrequest is open with 'asynchronous') because the html for each div is generated with php by the server. After the full page is loaded, including the divs, I need to execute…
1
vote
2 answers

Why is document.body null with my readyState logic?

I'm trying to append an iframe element into document.body as soon as possible. Error reports are coming in across all 4 major browsers (Chrome, Safari, FF, IE --various versions), we see document.body is null. I think this happens when my JS file…
theUtherSide
  • 3,338
  • 4
  • 36
  • 35
1
vote
4 answers

PHP never returns readyState 4 or status 200 in ajax

I have the following generic Ajax function: //run post request function ajaxPost (divid, parameters, file) { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6,…
YsoL8
  • 2,186
  • 5
  • 29
  • 47
1
vote
1 answer

document.readyState in Firefox 3.5.x

I've a site where I've putten this code to avoid errors: $(function() { var fnDocumentReady = function() { if(document.readyState != "complete") { setTimeout(function () { fnDocumentReady(); }, 300); return; } //do stuff …
Diego
  • 16,436
  • 26
  • 84
  • 136
1
vote
1 answer

Load ~ 400 (Bad Request). XmlHttpRequest works on local, but not on server-side

I am working on async load (using XMLHttpRequest .readystate & .responseText) of product table contents on pagination or filter changing. Funtions I wrote work perfectly but on local only. On apache/ngnix server-side it returns bad request. Please…
1
vote
4 answers

How to execute alert of the audio duration when data is available?

None of those tricks works for me... var audio=new…
1
vote
1 answer

Firefox Addon why readystate for tab gives undefined

I tried to make wait when open 4 websites until loading complete then get their title. For that I used readystate which didn't wait until load completes and gave Connecting as title. When I check in console the readystate gives undefined. Below is…
Dhay
  • 585
  • 7
  • 29
1 2 3
8 9