I'm trying to create an if-file-exists check on a URL.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else xhr = null;
return xhr;
}
function UrlExists(url) {
var http = createCORSRequest('HEAD', url);
http.onreadystatechange = function() {
if (this.readyState == this.DONE) {
if (this.status == 404) {
alert(url + " is NOT available!");
} else if (this.status != 0) {
alert(url + " is available!");
}
}
};
http.send();
}
UrlExists("http://bar.baz");
UrlExists("http://google.com");
</script>
Testing URLs
</body>
</html>
Currently getting a XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
error -- even when its running through Apache HTTPd on Mac OS 10.9.
Can you all help me to get this to work?