27

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();
  http.open('HEAD', url, false);
  http.send();
  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.

2
  • Is Firebug giving any errors? Hope you are doing XHR on your own server and not to another site (re. the same origin policy). I copy pasted the code in firebug and it works fine and returns true/false Commented Oct 12, 2010 at 16:14
  • 1
    I'm typing it in as urlExists("http://www.stackoverflow.com/"); and it doesn't work. Commented Oct 13, 2010 at 11:57

2 Answers 2

39

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

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

Usage:

UrlExists('/path/script.pl', function(status){
    if(status === 200){
       // file was found
    }
    else if(status === 404){
       // 404 not found
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

+1, but wouldn't you want to check for status codes other than 200 and 404? It could be a 500 or 304, etc
Is your if statement in your complete missing a {} ?
@Neil: indeed, it should be extended to check for more status codes. @Trip: no, you can do one statement without {}
Is this method still usable...? I'm getting a status 200 in firebug, but jQ status is 0 and reports a Cross Origin request error (my URL is in fact on another domain...but it's the 200 status I'm interested in, not any HTML)
What is cb? .
|
17

Modern, cross and short way

checkLink = async url => (await fetch(url)).ok

which is same of:

async checkLink(url) { return (await fetch(url)).ok }

which is same of:

async function checkLink(url) { return (await fetch(url)).ok }

which is same of:

Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), Blink 39.0, and Edge 13, synchronous requests on the main thread have been deprecated due to their negative impact on the user experience.

Cross solution (old, deprecated)

function UrlExists(url) {
 var http = new XMLHttpRequest();
 http.open('HEAD', url, false);
 http.send();
 return http.status==200;
}

2 Comments

Could you suggest anything if I try to check the 3rd party site and get the error: Access to fetch at 'https://3rd.party.site' from origin 'http://localhost:8080' has been blocked by CORS policy?
@MegaCasper this is a common problem but there is no relation with the question. Mostly site needs explicity allow you to access his sites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.