Reputation: 77
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 ?
Upvotes: 2
Views: 129
Reputation: 1075447
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();
Upvotes: 5
Reputation: 16041
You can make an AJAX request with XMLHttpRequest
, but instead of POST
or GET
, you should use the HEAD
HTTP verb.
Upvotes: 1