2

I want to make a web page request only if that web page is available. I have written my app using angularjs + javascript. Is there any way to determine whether a webpage is available or not using javascript ?

  • possible duplicate of [How to check if page exists using Javascript](http://stackoverflow.com/questions/3922989/how-to-check-if-page-exists-using-javascript) – Alex Booker Aug 03 '15 at 06:17

2 Answers2

5

If the page in question is on a different origin, you can't without using a server somewhere or relying on the other page implementing Cross-Origin Resource Sharing and supporting your origin, because of the Same Origin Policy.

If the page in question is on the same origin, you can do an ajax call to query it:

var xhr = new XMLHttpRequest();
xhr.open("HEAD", url);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            // It worked
        } else {
            // It didn't
        }
    }
};
xhr.send();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You can make an AJAX request with XMLHttpRequest, but instead of POST or GET, you should use the HEAD HTTP verb.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63