Created
September 7, 2017 06:21
-
-
Save myvyang/c3d0163f8d4f30ecf5f3ce9215afa06a to your computer and use it in GitHub Desktop.
absolute url
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 absolute(location, relative) { | |
if (relative.startsWith("http://") | |
|| relative.startsWith("https://") | |
|| relative.startsWith("data:")) { | |
return relative; | |
} | |
if (relative.startsWith("//")) { | |
return location.protocol + relative; | |
} | |
var prefix = location.protocol + "//" + location.host + "/"; | |
if (relative.startsWith("/")) { | |
return prefix + relative; | |
} | |
var paths = location.pathname.split("/"); | |
paths.pop(); | |
if (relative.startsWith(".")) { | |
var relpaths = relative.split("/"); | |
var filename = relpaths.pop(); | |
for (var i = 0; i < relpaths.length; i++) { | |
if (relpaths[i] == '.') | |
continue; | |
if (relpaths[i] == "..") | |
paths.pop(); | |
else | |
paths.push(relpaths[i]) | |
} | |
return prefix + paths.join("/") + "/" + filename; | |
} | |
return prefix + paths.join("/") + "/" + relative; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment