Assuming that I am on a page in a different domain (mydomain.com) and that the relative URL exists only in the code (not in the DOM)
How to combine two arbitrary URLs completely in 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/');
It should also work for
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/');
EDIT:
I am looking for a completely general function to combine an absolute URL with an arbitrary URL. The same logic as the browser is used to resolve links, but for URLs that are not in the HTML page of the page and / or do not refer to the current location .href.
var a = 'http://example.com/a/b/c/';
var b = '../d/e/';
assert(c == 'http://example.com/a/b/d/e/')
OR
var b = '/f/g/';
assert(c == 'http://example.com/f/g/')
OR
var b = 'http://jquery.com/h/i/';
assert(c == 'http://jquery.com/h/i/')
EDIT 2:
node.js URL, , . ( node.js )
, , , . Hackety hack