What makes relative url absolute using arbitrary root in javascript

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

+5
5

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/'));

+2

, NodeJS,

var url = require('url');
url.resolve(from, to);

:

var a = 'http://example.com/some/path/';
var b = '../other/path/';
var c = url.resolve(a, b);
assert(c == 'http://example.com/some/other/path/');

var a = 'http://example.com/some/path/';
var b = 'http://pink-unicorns.com/some/other/path/';
var c = url.resolve(a, b);
assert(c == 'http://pink-unicorns.com/some/other/path/');
+2

var magicUrlCombine = function(a,b){
   return (a + b).replace(/[\w\-\.]+\/..\/|\:\/\/[\w\-\.\/]+http/g,'');
}

,

http://jsfiddle.net/8HLeQ/2/

+1

, :

function magicCombine(a,b){
    if(b.indexOf('://') != -1) return b;

    var backs = 0;
    var lastIndex = b.indexOf('../');

    while(lastIndex != -1){
        backs++;
        lastIndex = b.indexOf('../', lastIndex+3);
    }

    var URL = a.split('/');
    //Remove last part of URL array, which is always either the file name or [BLANK]
    URL.splice(URL.length-1, 1)

    if(b.substr(0,1) == '/')
        b = b.substr(1);
    var toAdd = b.split('/');

    for(var i = 0, c = toAdd.length-backs; i < c; ++i){
        if(i < backs)
            URL[URL.length - (backs-i)] = toAdd[backs+i];
        else
            URL.push(toAdd[backs+i]);
    }

    return URL.join('/');
}

Gotta take care of all the cases ...

+1
source

I assumed that I understood the question, but my violin returns two false ones. The examples are not obvious.

http://jsfiddle.net/mplungjan/z5SUn/

function magicUrlCombine(a,b) {
  var linkA = document.createElement('a');
  linkA.href = a;
  var linkB = document.createElement('a');
  linkB.href = b;
  return linkB.href.replace(linkB.host,linkA.host)
}
+1
source

All Articles