Skip to content

Instantly share code, notes, and snippets.

@supereggbert
Created April 19, 2011 09:48
Show Gist options
  • Save supereggbert/927074 to your computer and use it in GitHub Desktop.
Save supereggbert/927074 to your computer and use it in GitHub Desktop.
Work out path
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