+1  A: 

http://www.w3schools.com/TAGS/tag_base.asp

the base tag has two components href and target. Yours seems to be fine. coold you give some example of the links on which it is failing?

see http://ashita.org/StackOverflow/base_test.html for a demonstration. (my test)

Edit: see comments

function addBase(url) {
    var regex = /^(https?|ftp):\/\//;

    var a = Array.prototype.slice.call(document.getElementsByTagName('a'),0);
    var link = Array.prototype.slice.call(document.getElementsByTagName('link'),0);
    var script = Array.prototype.slice.call(document.getElementsByTagName('script'),0);
    var img = Array.prototype.slice.call(document.getElementsByTagName('img'),0);

    var hrefs = a.concat(link);
    var srcs = img.concat(script);

    var element,href,src;
    for (var i=0,len=hrefs.length;i<len;++i) {
        element = hrefs[i];
        href = element.getAttribute("href");
        if (href) {
            if (!regex.test(href)) {
                href = (url + "/" + href).replace("//","/"); //to handle double slash     collision
                element.setAttribute("href",href);
            }
        }
    }
    for (var i=0,len=srcs.length;i<len;++i) {
        element = srcs[i];
        src = element.getAttribute("src");
        if (src) {
            if (!regex.test(src)) {
                src = (url + "/" + src).replace("//","/"); //to handle double slash     collision
                element.setAttribute("src",src);
            }
        }
    }
}

Tested and working in firefox

Jonathan Fingland
Such as this "<link href="templates/style.css" type="text/css" rel="stylesheet"/>" ... it looks inside "mysite.com/" instead of "mysite.com/folder/" so it does not find the CSS file.
Jenko
firebug still shows the url as relative, but looking at the target url in the status bar, I see the expected address
Jonathan Fingland
Funny how even your base element shows greyout out in my Firebug, I'm adding the base element dynamically with JS, if thats the problem.
Jenko
I ran a test on stackoverflow.com and adding the base element via firebug does not grant any benefit. It appears it isn't "scriptable". Looks like you'd have to include it in the original html. (if adding via javascript you could loop through the elements and add it that way, instead of using base)
Jonathan Fingland
"loop through the elements"? In other words?
Jenko
document.getElementsByTagName(...), loop through every element in the node list, do a regex test on each to check if it includes the protocol in the address, and if not, add your "base" href to the beginning of each one. will edit answer.
Jonathan Fingland
Thanks man! Damn good idea.
Jenko