JSFiddle - React, Tailwind, and code Playground

JavaScript

function absolutize(base, url) {
    d = document.implementation.createHTMLDocument();
    b = d.createElement('base');
    d.head.appendChild(b);
    a = d.createElement('a');
    d.body.appendChild(a);
    b.href = base;
    a.href = url;
    return a.href;
}

console.log(absolutize('http://google.com/test.html', 'other.html'))
console.log(absolutize('http://google.com/foo/bar/test.html', 'other.html'))
console.log(absolutize('https://google.com/foo/bar/test.html', '/other.html'))
console.log(absolutize('http://google.com/foo/bar/test.html', 'https://example.com/other.html'))