Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect HTTP status from JavaScript

I want to detect in a script, which could be deployed beyond my control, whether the page was delivered with a HTTP status of 200, 404 or 500 etc.

This can't be done, right?

like image 624
EoghanM Avatar asked May 07 '09 21:05

EoghanM


People also ask

How do I check my HTTP status?

Just use Chrome browser. Hit F12 to get developer tools and look at the network tab. Shows you all status codes, whether page was from cache etc.

How do I get a status code response?

To get the status code of an HTTP request made with the fetch method, access the status property on the response object. The response. status property contains the HTTP status code of the response, e.g. 200 for a successful response or 500 for a server error. Copied!

Which HTTP status code do we get when the website loads successfully?

These are the HTTP status codes that have the greatest impact on UX and SEO: 200 – Ok – the page is successfully loaded – normal. 201 – Created (see more details below) 301 – Permanent redirect.

What is an HTTP status?

An HTTP status code is a message a website's server sends to the browser to indicate whether or not that request can be fulfilled. Status codes specs are set by the W3C. Status codes are embedded in the HTTP header of a page to tell the browser the result of its request.


1 Answers

Have the page make a XMLHttpRequest to itself (using location.href or document.URL) and check the HTTP status you get. Seems to be pretty portable way to me. Why you would do a thing like this is beyond my understanding though ;)

like image 180
anddoutoi Avatar answered Oct 13 '22 16:10

anddoutoi