Checking if a URL is broken in Javascript

This question has been posted on Stack before, but none so specific as to what I'm trying to understand.

The simplest way to check if a URL is corrrect to send a http Head request. But how do you use that to specify the URL ?

I found this in a previous post :

function UrlExists(url) {
  var http = new XMLHttpRequest();'HEAD', url, false);
  return http.status!=404;

But it doesn't seem to run in firebug for a few reasons.

My apologies ahead of time for being daft.



I'd recommend to use jQuery for correct cross-browser ajax requests:

function UrlExists(url, cb){
        url:      url,
        dataType: 'text',
        type:     'GET',
        complete:  function(xhr){
            if(typeof cb === 'function')
               cb.apply(this, [xhr.status]);


UrlExists('/path/', function(status){
    if(status === 200){
       // file was found
    else if(status === 404){
       // 404 not found


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.