Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check if url returns 404 error status

 
abida khatoon
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

can do this either by Jquery or Javascript...

This is what i have done so far...
function checkURL(url) {
var urlregex = new RegExp("^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
if (urlregex.test(value)) {
alert("valid url");
var request = new XMLHttpRequest;
request.open('GET', url, false);

request.onreadystatechange = function(){
if(request.readyState==4){
console.log(request.readyState);
return true;
}else{
return false;
}
}
request.send();
}
alert("invalida url pattern");
return (false);
}


but this return status =0 for all...!!!
any suggestions and help would be appreciated..

 
E Armitage
Rancher
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erm, you get the status code from the status property on the jqXHR, see the docs here: http://api.jquery.com/jQuery.ajax/
I'm not sure what all that regex is intending to do.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, get rid of that regexp. I guarantee you that it's incorrect for more than one reason (for example, it disallows valid domain names). What's more, it's unnecessary, since the code returns "false" anyway if the URL does not exist.
 
abida khatoon
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea!!! I m not focusing on validating url...i.e i will not be going with regex for now..!!!
i have to do something like cross domain (accessing data from different sever machine).How can this be done.The page i m looking for is on another server and have see whether its existing or not..I need some help on this..

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64193
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will be difficult to do in general from the client. Any reason you're not doing this from the server (where it's a lot easier)?
 
abida khatoon
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to do it from client side..!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic