Created
October 28, 2020 16:50
-
-
Save adrielTosi/864af28af41aaf7ad8eef4d715a1ee9b to your computer and use it in GitHub Desktop.
Check if Url is absolute or relative : has base URL or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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