File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 761
    
    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: 39547
    
  27
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 - updated with new look and Ping home screen widget
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: 60050
    
  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..!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check if url returns 404 error status
 
Similar Threads
alphanumeric validation using regular expression
PHP Send Form to Email
problem with regular expression for date formate
Back Button issue in browser after logout
Recieving 'null' values from a jsp page in Struts2 Action