Skip to content

Instantly share code, notes, and snippets.

@adrielTosi
Created October 28, 2020 16:50
Show Gist options
  • Save adrielTosi/864af28af41aaf7ad8eef4d715a1ee9b to your computer and use it in GitHub Desktop.
Save adrielTosi/864af28af41aaf7ad8eef4d715a1ee9b to your computer and use it in GitHub Desktop.
Check if Url is absolute or relative : has base URL or not
function isUrlAbsolute(url) {
if (url.indexOf("//") === 0) {
return true;
} // URL is protocol-relative (= absolute)
if (url.indexOf("://") === -1) {
return false;
} // URL has no protocol (= relative)
if (url.indexOf(".") === -1) {
return false;
} // URL does not contain a dot, i.e. no TLD (= relative, possibly REST)
if (url.indexOf("/") === -1) {
return false;
} // URL does not contain a single slash (= relative)
if (url.indexOf(":") > url.indexOf("/")) {
return false;
} // The first colon comes after the first slash (= relative)
if (url.indexOf("://") < url.indexOf(".")) {
return true;
} // Protocol is defined before first dot (= absolute)
return false; // Anything else must be relative
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment