Created
July 23, 2013 17:07
-
-
Save davidrautert/6064150 to your computer and use it in GitHub Desktop.
Provided a url and a path relative to that url, this function will return the resulting absolute url
This file contains hidden or 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
var getAbsoluteUrl = function(url, path) { | |
if(url.substr(-1) == '/') { | |
url = url.substr(0, url.length-1); | |
} | |
var path = path; | |
if(path !== null) { | |
if(url.substr(-1) == "/") { | |
url = url.substr(0, url.length-1); | |
} | |
while(/^\.{1,2}\//i.test(path)) { | |
if(/^\.{1}\//i.test(path)) { | |
path = path.substr(2,path.length); | |
} else { | |
var url_arr = url.split('/'); | |
url_arr.pop(); | |
url = url_arr.join("/"); | |
path = path.substr(3,path.length); | |
} | |
} | |
url = url + "/" + path; | |
} | |
return url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment