Created
April 19, 2011 09:48
-
-
Save supereggbert/927074 to your computer and use it in GitHub Desktop.
Work out path
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
var getAbsolutePath=function(path,relativeto){ | |
if(!relativeto) relativeto=window.location.href; | |
if(!relativeto) return path; | |
//remove ? and # from relative to | |
if(~relativeto.indexOf("?")){ | |
relativeto=relativeto.substr(0,relativeto.indexOf("?")); | |
}else if(~relativeto.indexOf("#")){ | |
relativeto=relativeto.substr(0,relativeto.indexOf("#")); | |
} | |
var pathParts=path.split("/"); | |
if(~pathParts[0].indexOf(":")) return path; | |
var relativeParts=relativeto.split("/"); | |
var domain=""; | |
if(~relativeParts[0].indexOf(":")){ | |
domain+=relativeParts.shift()+"/"+relativeParts.shift()+"/"+relativeParts.shift()+"/"; | |
} | |
if(pathParts[0]==""){ | |
pathParts.shift(); | |
return domain+pathParts.join("/") | |
} | |
relativeParts.pop(); | |
for(var i=0;i<pathParts.length;i++){ | |
var part=pathParts[i]; | |
if(part==".."){ | |
relativeParts.pop(); | |
}else if(part!="."){ | |
relativeParts.push(part); | |
} | |
} | |
return domain+relativeParts.join("/"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment