http status code 404 - if-URL-exists check with javascript -


i'm trying create if-file-exists check on 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 + " not available!");       } else if (this.status != 0) {         alert(url + " available!");       }     }   };   http.send(); }  urlexists("http://bar.baz"); urlexists("http://google.com"); </script> testing urls </body> </html> 

currently getting xmlhttprequest cannot load http://google.com/. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost' therefore not allowed access. error -- when running through apache httpd on mac os 10.9.

can me work?

this cross origin issue. have use proxy. example, i've used yql. try following:

function isvalidurl(url) {     var encodedurl = encodeuricomponent(url);     var isvalid = false;      $.ajax({       url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3d%22" + encodedurl + "%22&format=json",       type: "get",       async: false,       datatype: "json",       success: function(data) {         isvalid = data.query.results != null;       },       error: function(){         isvalid = false;       }     });      return isvalid; } 

the usage trivial:

var isvalid = isvalidurl("http://www.wix.com"); alert(isvalid ? "valid url!!!" : "damn..."); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -