Get current page http status from javascript

JavascriptHttp

Javascript Problem Overview


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)

Javascript Solutions


Solution 1 - Javascript

This is not in any way possible, sorry.

Solution 2 - Javascript

Yes You can

Simply request the same page, i.e. URI, using the XMLHttpRequest. Suppose that your page on /stop.php in stop.php you may do something like:

<script>
function xhrRequest(){            
            console.log(this.status);
            // Do some logic here. 
        }
function getReq(url){
            var oReq = new XMLHttpRequest();
            oReq.addEventListener("load", xhrRequest);
            oReq.open("GET", url);
            oReq.send();
        }
getReq("/stop.php");
</script>

Checkout this DEMO

Note:

> You have to note that, it is a copy of the page not the page itself. > I, already, have used this solution on a page in which the server may > generate Forbidden HTTP status code when the request is come from > unauthorized IP address, so the condition here is very simple and > there is no much difference between the original and the copy page > that you have simulate its visit.

Solution 3 - Javascript

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) });

Solution 4 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrea ZilioView Question on Stackoverflow
Solution 1 - JavascriptMartin JespersenView Answer on Stackoverflow
Solution 2 - JavascriptSaidbakRView Answer on Stackoverflow
Solution 3 - JavascriptDanilo SonciniView Answer on Stackoverflow
Solution 4 - Javascriptsaeed arab sheybaniView Answer on Stackoverflow