Skip to content Skip to sidebar Skip to footer

How To Get Status Code Of Remote Url Using Javascript/ajax But Not Using Jquery?

I am trying to check if a url is reachable or not using javascript without using jQuery or the method of loading an image from that url. It would be great if I can get the status c

Solution 1:

I'm not sure about how "cross-browser" this is:

<scriptsrc="http://www.google.com"onerror="alert('error')"></script><!-- Is Fine --><scriptsrc="http://www.notrealnotrealzz.com"onerror="alert('error')"></script><!-- Alerts Error -->

And it's obviously a hack, but hopefully it will help.

A more dynamic version:

var el = document.createElement('script');
el.onerror = errorFunction;
el.src = url;
document.body.appendChild(el);

Solution 2:

Create a server side script using a server side language, have this script return the status code. This "proxy" script, will request the URL for you, so that no cross domain requests need to be made.

For an example of a Python response code script, see here: What’s the best way to get an HTTP response code from a URL?

Hope that helps, -Sunjay03

Post a Comment for "How To Get Status Code Of Remote Url Using Javascript/ajax But Not Using Jquery?"