How to check if a url exists in javascript

I want to check if a url exists before attempting to load that url using ajax.

Related tags: JavaScript

Credit: own experience

function checkUrl(url) {
        var request = false;
        if (window.XMLHttpRequest) {
                request = new XMLHttpRequest;
        } else if (window.ActiveXObject) {
                request = new ActiveXObject("Microsoft.XMLHttp");
        }

        if (request) {
                request.open("GET", url);
                if (request.status == 200) { return true; }
        }

        return false;
}

Rating: 3

Propose a different solution...