aspose 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 Java 8 in Action this week in the Java 8 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: 761
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: 39578
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

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: 60082

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..!!!
It is sorta covered in the JavaRanch Style Guide.
subject: how to check if url returns 404 error status
Similar Threads
PHP Send Form to Email
Recieving 'null' values from a jsp page in Struts2 Action
alphanumeric validation using regular expression
Back Button issue in browser after logout
problem with regular expression for date formate