i want to get url relative to another url
for example
"../d.html".relativeTo("/a/b/c.html"); //==> "/a/d.html"
"g.html".relativeTo("/a/b/c.html"); //==> "/a/b/g.html"
"./f/j.html".relativeTo("/a/b/c.html"); //==> "/a/b/f/j.html"
"../../k/w.html".relativeTo("/a/b/c.html"); //==> "/k/w.html"
"http://www.google.com".relativeTo("/a/b/c.html"); //==> "http://www.google.com"
i think there is a simple solution for that because browsers does it for relative url links.
i have tried
String.prototype.relativeTo=function(input){
if(/^https?:\/\//i.test(this)) {
return this.valueOf();
}
else {
var a = document.createElement("a");
a.href = input.replace(/\w+\.\w+/, "") + this;
return a.href;
}
}
but it returns absolute url
is there some simple way to do that?