在javascript中使用任意根制作相对url绝对值

Jes*_*det 5 javascript url

假设我在不同域(mydomain.com)上的页面上,并且相对URL仅存在于代码中(不在DOM中)

如何在javascript中完全组合两个任意网址?

var a = 'http://example.com/some/path/';
var b = '../other/path/';
var c = magicUrlCombine(a,b);
assert(c == 'http://example.com/some/other/path/');
Run Code Online (Sandbox Code Playgroud)

它也应该有效

var a = 'http://example.com/some/path/';
var b = 'http://pink-unicorns.com/some/other/path/';
var c = magicUrlCombine(a,b);
assert(c == 'http://pink-unicorns.com/some/other/path/');
Run Code Online (Sandbox Code Playgroud)

编辑:

我正在寻找一个完全通用的功能,用于将绝对URL与任意URL组合.与浏览器用于解析链接的逻辑相同,但用于不在页面HTML中和/或不相对于当前location.href的URL.

var a = 'http://example.com/a/b/c/';
var b = '../d/e/';
assert(c == 'http://example.com/a/b/d/e/')
Run Code Online (Sandbox Code Playgroud)

要么

var b = '/f/g/';
assert(c == 'http://example.com/f/g/')
Run Code Online (Sandbox Code Playgroud)

要么

var b = 'http://jquery.com/h/i/';
assert(c == 'http://jquery.com/h/i/')
Run Code Online (Sandbox Code Playgroud)

编辑2:

node.js有一个具有正确功能的url模块,但我还没有找到一种在客户端重用它的好方法.(如何在客户端使用node.js模块系统)

我设法通过破解我的方式使其工作,但这不是一个真正的解决方案我觉得放入生产网站很舒服.Hackety hack

Jes*_*det 2

JQuery Mobile 有它

$.mobile.path.makeUrlAbsolute(relPath,absPath)

console.log($.mobile.path.makeUrlAbsolute('../d/e/', 'http://example.com/a/b/c/'));
console.log($.mobile.path.makeUrlAbsolute('/f/g/', 'http://example.com/a/b/c/'));
console.log($.mobile.path.makeUrlAbsolute('http://jquery.com/h/i/', 'http://example.com/a/b/c/'));
Run Code Online (Sandbox Code Playgroud)

都给出了预期的结果