3

Is it possible through jQuery (or plain javascript) to test if a webpage on another domain is available? I tried getting the response headers with an ajax-call but I get an error no matter what site outside my own domain I test.

So do I really need a proxy script on my server or would I be able to skip that request?

3 Answers 3

2

Is it possible through jQuery (or plain javascript) to test if a webpage on another domain is available?

Due to same origin policy restriction you need a proxy/bridge on your server unless the remote server implements JSONP which obviously we cannot assume for the general case.

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

Comments

2

You can create an <img> tag that points to an existing image on the external domain.

If the onerror event fires, image, and perhaps the entire site, is down.

If it fires after 5 seconds or so, it probably timed out, so the entire site is likely to be down.

4 Comments

What if there is no image on the remote domain? The onerror will fire and yet this is not an indication that the domain is not alive.
@Darin: That's why I wrote (or the image).
in this case you shouldn't write the site (or the image) is down, you should write the image is down as that's the only objective conclusion we could make provided this observation (sorry for my nitpicking :-)).
@DarinDimitrov It does not necessarily have to be an image to load as an image. It will be corrupt as an image, still valid as an HTTP request. For example, <script> tag works fine in this case. It will fire a SyntaxError as HTML gets parsed instead of the expected JS, but onerror won't get called, unless there's a problem with the request indeed. Bad news is, in IE it does not work: onerror is never invoked. In Firefox, it works just fine. Another bad news is, due to the event-driven nature of this solution, one can't write a synchronous isWorking(url) function.
0

Yes, you need to use a proxy script on your server. JavaScript cannot be used in a browser to request resources across domains, as per the same-origin policy.

4 Comments

I believe I read somewhere that you could get access to the headers - and that's really all I need. If that isn't possible why is that? Would it be a security risk if I could see if the response was 200 or not?
This, taken by word, is not true. You can request resources across domains; just think about iframes, img tags with src pointing to another domain, or styles and scripts (e.g. jQuery included from remote location). Actually there is a way around to see if a site is available, however I couldn't get it to work with MSIE yet.
@Powerslave this answer is really just out of date: en.wikipedia.org/wiki/Cross-origin_resource_sharing
@MattBall It might and might as well not be the case. CORS is not a proper standard yet and there might be a need to have some kind of workaround for browsers that do not support CORS. Also, you might not have control over the source/behaviour settings of the remote site and still need to check if it's alive or not.

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.