Ad

Check Whether Address Path Is Available

- 1 answer

Sorry, it might be duplicated post. is there a way can javascript/jquery to detect whether the url link is available or not ?? for instances, detect http://www.thisissample.com/ is not available and return false, if https://www.google.com.my/ is available and return true.

Ad

Answer

So, since you could run into problems if making an ajax request because of cross-domain restrictions, another strategy would be trying to load the url into a script tag, which does accept other domains. Here is what your code would look like:

function checkURL(url) { 
    var scriptTag = document.body.appendChild(document.createElement("script"));
    scriptTag.onload = function() {
        alert( url + " is available");
    };
    scriptTag.onerror = function() {
        alert( url + " is not available");
    };
    scriptTag.src = url;
} 
Ad
source: stackoverflow.com
Ad