Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js check link if exists

//checking link
function urlExists(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            callback(xhr.status < 400);
        }
    };
    xhr.open('HEAD', url);
    xhr.send();
}


// run function
function check_link(url) {
    urlExists(url, function (exists) {
        if (exists) {
            console.log('"%s" exists?', url, exists);
        }
    });
}
 
PREVIOUS NEXT
Tagged: #js #check #link #exists
ADD COMMENT
Topic
Name
9+6 =