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
2
votes
1 answer

AJAX onreadystatechange function not always called

I have a AJAX call to retrieve some XML using the below method. Often when I run the code it does not enter the onreadystatechange function until the last iterations of my foreach loop. I'm assuming this is because the calls to "www.webpage.com/" +…
Nick
  • 179
  • 4
  • 13
2
votes
1 answer

Selenium waitForDOMToLoad()?

Selenium's open command implicitly waits for the whole page to load, including images, etc. How do I just wait for the DOM to load, like jQuery's .ready() method? Currently, I'm using waitForElementPresent, but is there a better alternative?
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
2
votes
1 answer

DOMReady: 'kendo' is undefined

I am receiving an intermittent error from JavaScript in my production environment and was hoping to learn more about the loading process. The error is simple enough to understand: 'kendo' is undefined. It states that the kendo variable is…
jeffjenx
  • 17,041
  • 6
  • 57
  • 99
2
votes
3 answers

jQuery get "readyState" property of video

As title,I cannot get DOM attrbute by jQuery.attr() But can get attrbute by document.attrbute. How to solve It? console.log($("#video01").attr('readyState')); console.log($("#video01")[0].readyState);
2
votes
1 answer

VB Form Filling

I may be missing a very basic thing, but i just dont understand why this isnt working. I am looking to fill out some web forms based on some user input. I have already written this in vbscript where its working flawlessly. Now i want to import it to…
Gematria
  • 105
  • 1
  • 14
2
votes
2 answers

readyState event for HEADERS_RECEIVED using jQuery.ajax

Using the native XMLHttpRequest object, it's possible to add an event listener to the onreadystatechange event, and be notified when the readyState is 2, or HEADERS_RECEIVED. This is useful, as this enables the reading of the HTTP response headers…
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
2
votes
1 answer

Phonegap jQuery.ajax requests works on AVD but fails on real device

I have this annoying problem on my app PhoneGap that the request below works fine on my web browser and my AVD, but fails on device, I keep getting readyState 0, can anyone help? var appCredentials =…
vrsbrazil
  • 21
  • 1
2
votes
2 answers

VBA Excel WebBrowser how to reset ReadyState before .Click from the code?

I have similar code in Excel VBA with WebBrowser control inserted in a form: basePath = "http://www.amazon.co.uk/" ' Open main website (Home page) WebBrowser.Navigate basePath PauseTime = 30 ' Set duration to wait in seconds. Start = Timer ' Set…
Nikolay Ivanov
  • 5,159
  • 1
  • 26
  • 22
2
votes
0 answers

Ajax ready state not stuck on 1

after searching the internet I was unable to find an answer as to why my AJAX code is not working. My assignment is to retrieve a text file and display it to the browser using AJAX but the ready state stops at 1. an example file is canada.txt and is…
2
votes
1 answer

100% CPU consumption while establishing WebSockets connection

I try to write a function which connects to the WebSocket server. The problem occurs when this server is down. This function should wait for it, even for long time, but it shouldn't consume 100% of CPU. Here is my code: var state = { connected:…
ciembor
  • 7,189
  • 13
  • 59
  • 100
2
votes
5 answers

Ajax - Function to check if Url exists

I'm building a building a website using the Reddit API to display images from Reddit. I'm getting the data via JSON then use it to build the page, using URLs provided as sources for the images. Some of the URLs that I get don't go to images…
Simon Be
  • 199
  • 2
  • 2
  • 9
2
votes
2 answers

JavaScript create images and document.readyState

To make sure a document is ready before doing stuff, i do the following : (function() { var interval = window.setInterval(function() { if("complete" === document.readyState) { window.clearInterval(interval); //…
Virus721
  • 8,061
  • 12
  • 67
  • 123
2
votes
1 answer

how to use DocumentComplete Event with Gecko Webbrowser c#/.net

I'm creating an application that contains "geckoWebBrowser" in c #. But I have to wait the complete loading a web page, and then continue to execute other instructions. Please help me with my code: private void button1_Click(object sender, EventArgs…
Jonas Gobel
  • 153
  • 3
  • 5
  • 13
2
votes
1 answer

Can I manually check the value of readyState of a browser?

I have a page where RFT (Rational Functional Tester) tells me that readyState is 3 for IE. Visually it looks like the loading of this page finished. Can I somehow manually check the readyState value for a page in firefox or chrome?
Radek
  • 13,813
  • 52
  • 161
  • 255
1
vote
3 answers

Windows 7: network not ready (blue circle is spinning)

On my Win7-PC the network takes up to 10 minutes until it's shown as ready (through network icon). The weird thing is that even in the meantime (blue circle is spinning) network activities like mail, internet and access to network shares are…
Dunken
  • 8,481
  • 7
  • 54
  • 87
1 2
3
8 9