I want to validate a url address actually returns a valid page.
There are two approaches one could take.
- IFrame - create and iframe that points to the url
- Ajax - create an ajax request to the url and look at the status codes - Here is some fiddling
The Ajax method is not working because it always returns a status code of 0 for cross domain requests whether the page is there or not.
The IFrame method is not working b.c. I can not find a mechanism for capturing status or errors of the frame.
Most of the google hits I'm getting are for syntax checking.
Fiddle Code for Ajax
var urlTest = function (url) {
var xhr = new window.XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
console.log('readyState | status : ' + this.readyState + ' | ' + this.status);
if (this.readyState === 4) {
if (this.status === 200) {
// console.log('4 | 200');
// xhr.responseText;
}
}
};
xhr.send(null);
}
urlTest('http://www.google.com'); // cross domain always give status 0