Reputation: 26954
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?
Upvotes: 17
Views: 16881
Reputation: 10111
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 ;)
Upvotes: 9
Reputation: 17984
I recently found how to do it (credits to : hunlock)
AJAX.getAllResponseHeaders() -- returns as a string all current headers in use. AJAX.getResponseHeader("headerLabel") -- returns value of the requested header.
Upvotes: -3
Reputation: 491
Why does the javascript need to know this? Sounds like it would make more sense just to embed it on a custom 404 page. No need to detect HTTP status of the parent page (I don't think it's possible - maybe doing an ajax call to itself after each load, but that's just silly). if the code is being executed, its guaranteed to be a 404
Upvotes: -2
Reputation: 27443
Page A can be a Javascript that loads page B via AJAX and displays it with document.write or in a pop up window or however.
In such a strategy, you can check return code for success/failure in the AJAX handler and send different things to the output window depending on status.
Most Ajax libraries provide a way to examine the return code....
See for instance "transport.status" with Ajax.Request in Prototype.js
Upvotes: 1