permaculture playing cards*
The moose likes HTML, CSS and JavaScript and the fly likes how to check if url returns 404 error status Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how to check if url returns 404 error status" Watch "how to check if url returns 404 error status" New topic
Author

how to check if url returns 404 error status

abida khatoon
Greenhorn

Joined: Oct 25, 2013
Posts: 7
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

Joined: Mar 17, 2012
Posts: 892
    
    9
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
Marshal

Joined: Mar 22, 2005
Posts: 41133
    
  45
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.


Ping & DNS - my free Android networking tools app
abida khatoon
Greenhorn

Joined: Oct 25, 2013
Posts: 7
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

Joined: Jan 10, 2002
Posts: 60791
    
  65

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)?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
abida khatoon
Greenhorn

Joined: Oct 25, 2013
Posts: 7
I have to do it from client side..!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to check if url returns 404 error status
 
Similar Threads
Back Button issue in browser after logout
alphanumeric validation using regular expression
Recieving 'null' values from a jsp page in Struts2 Action
problem with regular expression for date formate
PHP Send Form to Email