How to enable IE to support base html tag with relative href? for example: `<base href =" ../ "> </base>`
I know, according to the specification, hrefof baseshould be an absolute URI.
href = uri [CT]
This attribute specifies an absolute URI that acts as the base URI for resolving relative URIs.
But workarounds with firefox and chrome support, for example. ../../which is very important for my current project. I did not find a better solution than the "href relative base" for my project.
Are there any hacks or jobs for IE to support relative URIs? Now my web pages work well in firefox and chrome, but it should support IE.
Use this function to convert your URL to an absolute value:
function toAbsURL(s) {
var l = location, h, p, f, i;
if (/^\w+:/.test(s)) {
return s;
}
h = l.protocol + '//' + l.host + (l.port!=''?(':' + l.port):'');
if (s.indexOf('/') == 0) {
return h + s;
}
p = l.pathname.replace(/\/[^\/]*$/, '');
f = s.match(/\.\.\//g);
if (f) {
s = s.substring(f.length * 3);
for (i = f.length; i--;) {
p = p.substring(0, p.lastIndexOf('/'));
}
}
return h + p + '/' + s;
}
var base = document.getElementsByTagName('base')[0];
base.href = toAbsURL(base.href);
, IE. .location, href base, .
<!--[if IE]>
<script type="text/javascript">
var base = document.getElementsByTagName('base')[0];
base.href = toAbsURL(base.href);
</script>
<![endif]-->
ps: <base /> - , .
, , .