61

Is there any way to get the http status of the current web page from javascript?

Spent some time searching on the web, but no luck at all... Seems like it's not possible, but wanted to check with Stack Overflow for maybe some fancy workaround.

(Providing it from the server as part of the response body is not acceptable, the status is supposed to be only available via the http header)

Dharman
  • 21,838
  • 18
  • 57
  • 107
Andrea Zilio
  • 4,204
  • 3
  • 26
  • 34

3 Answers3

43

This is not in any way possible, sorry.

Martin Jespersen
  • 23,663
  • 6
  • 52
  • 66
-4

you can only check status of page loading try:var x = document.readyState; The result of x could be: One of five values:

uninitialized - Has not started loading yet
loading - Is loading
loaded - Has been loaded
interactive - Has loaded enough and the user can interact with it
complete - Fully loaded
  • 5
    `document.readyState` refers to the loading state of the document, not to the page's HTTP status. – csvoss Jun 11 '18 at 22:08
-4

It is not beautiful, but you can use:

t = jQuery.get(location.href)
    .success(function () { console.log(t.status) })
    .error(function()    { console.log(t.status) });
Martin Tournoij
  • 23,567
  • 24
  • 90
  • 127
  • 3
    Incorrect: this code makes a new requests, it doesn't get the status of the page as it is currently loaded in the browser. As of 2018, there's no way to get the error status of the currently loaded HTML page for any page. – Eric Nov 06 '18 at 09:03