17

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?

1
  • You could look at the page contents to see if it contains phrases like "not found" or "error", but that's probably the best you can do. Commented Dec 24, 2014 at 13:08

4 Answers 4

9

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

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

5 Comments

Yes this is the solution I ended up going for - additionally I made a HTTP HEAD request to forestall having the client download the entire page twice. Why I want to do this? Think a .js file that I'm writing for website administrators to install on their website.
This is dangerous, because the second request might alter the server in unexpected ways (but double-POSTing the page), etc. Also, the same request might succeed once, but fail the next time.
I'm confused, neither @EoghanM or me mentions POST. He even says he used HEAD. Using GET or HEAD should be safe as they are idempotent methods and should not cause side-effects.
"Why you would do a thing like this?" - because your 404 page is the same as another page, and you want to handle the small differences on the client. ;)
I want to set the <base href> when the page is a 404, so I need to do this early and synchronously. Annoying if it will slow down when the page is a 200 though.
1

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

2 Comments

It sounds like you are on the right track with transport.status - ideally I'd like to avoid another HTTP request, but maybe this is the only way?
I'm not experienced enough to know if this is the ONLY way or not. If you use the answer I gave, you will also need to obey Javascript's security model and fetch pages ONLY from the same site. This can be gotten around, though, by clever uses of proxies.
-2

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

2 Comments

I can't create a custom 404 page as the script is 'being deployed beyond my control' - I'm not making the HTML and the 404 page won't be on my site :)
Making a browser extension and running into the same issue - if the current URL 404s and is a misspelling of a common page on a certain site, I'm trying to redirect to that page. I'm going with the AJAX solution, since I don't believe a page's deletion status would change often in the milliseconds between the first and second request.
-3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.