79

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)

7
  • 2
    well, given that your JS is executed at all, I suppose the http response code was 200 OK? Commented Feb 1, 2013 at 20:26
  • 6
    @AndreasGrapentin That's but one of many possibilities Commented Feb 1, 2013 at 20:26
  • 11
    @AndreasGrapentin You could have a custom 404 page with some special javascript.. Commented Feb 1, 2013 at 20:27
  • 1
    304 is also an acceptable status code to get javascript running Commented Feb 1, 2013 at 20:27
  • 1
    Wow. I didn't know that. anyway, I think that's a duplicate of stackoverflow.com/questions/837064/… Commented Feb 1, 2013 at 20:28

5 Answers 5

56

This is not in any way possible, sorry.

Sign up to request clarification or add additional context in comments.

1 Comment

12

This is (experimental but) possible in chrome by leveraging responseStatus from Performance API. We look for the navigation entryType which should provide the page http status.

const navigationData = window.performance.getEntries().find(e => e.entryType === "navigation")
navigationData.responseStatus // 200

Alternative:

const navigationData = window.performance.getEntriesByType('navigation')[0];
navigationData.responseStatus // 200

3 Comments

Note that instead of window.performance.getEntries().find(e => e.entryType === "navigation") you can just do window.performance.getEntriesByType('navigation')
.find returns the first element found, while getEntriesByType returns an array, so we'd need to look at index [0]. Have updated answer with the alternative. Cheers!
Now supported in Firefox as well, Safari is the only mainstream holdout: developer.mozilla.org/en-US/docs/Web/API/…
3
console.log(window.performance.getEntries()[0].responseStatus)

1 Comment

Answer needs supporting information Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Update 2024:

If you need a reliable (currently chrome based only) solution use https://stackoverflow.com/a/76918876/2590616 which is the best choice imho.

Alternatively

Include the status code on the server side, then read it on the client side via JavaScript, e.g.:

a) Java + Thymeleaf + meta tag:

<meta name="statuscode" th:content="${#response.status}">

<script>
  const statusCode = document.getElementsByName("statuscode").getAttribute("content");
</script>

b) Java + Thymeleaf + JavaScript variable:

<script th:inline="javascript">
  const statusCode = [[${#response.status}]];
</script>

c) PHP + meta tag (unverified):

<meta name="statuscode" content="<?php echo http_response_code() ?>">

<script>
  const statusCode = document.getElementsByName("statuscode").getAttribute("content");
</script>

Comments

-1
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.