File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Murach's Java Servlets and JSP this week in the Servlets 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

how to check if url returns 404 error status

abida khatoon

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;'GET', url, false);

request.onreadystatechange = function(){
return true;
return false;
alert("invalida url pattern");
return (false);

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

E Armitage

Joined: Mar 17, 2012
Posts: 888
Erm, you get the status code from the status property on the jqXHR, see the docs here:
I'm not sure what all that regex is intending to do.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41052
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

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..

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60752

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

Joined: Oct 25, 2013
Posts: 7
I have to do it from client side..!!!
wood burning stoves
subject: how to check if url returns 404 error status
Similar Threads
alphanumeric validation using regular expression
Recieving 'null' values from a jsp page in Struts2 Action
Back Button issue in browser after logout
problem with regular expression for date formate
PHP Send Form to Email