Question

I'm adding the base URL tag to the document head using JS, so the relative links on the page work. But it does not take effect, and Firebug (debugging addon for Firefox) shows the <BASE /> element greyed out.. why? Does this mean Firefox cannot understand it or the syntax is incorrect?

Image http://www.freeimagehosting.net/uploads/a3122c1ddd.png

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top