Usare questa funzione per convertire l'URL della assoluta:
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;
}
Si potrebbe utilizzare
var base = document.getElementsByTagName('base')[0];
base.href = toAbsURL(base.href);
Esempio http://jsfiddle.net/tEpkx/1/
parte il fatto che si deve rilevare il browser, anche, e eseguirlo solo per IE. Altri browser otterranno window.location aggiornato automaticamente dall'href del tag base
e questo frammento di codice lo cambierà di nuovo. Quindi scrivere come
<!--[if IE]>
<script type="text/javascript">
var base = document.getElementsByTagName('base')[0];
base.href = toAbsURL(base.href);
</script>
<![endif]-->
ps: <base />
è un singolo tag, non richiede la chiusura di uno.
Aggiornamento per includere il numero di porta se è impostato.
Ho trovato per IE che href di base funziona bene fino a quando non si tenta di salire di un livello. – Joshua
Ma ho bisogno che salga per diversi livelli – Freewind
Vengono generate pagine html statiche. Ho bisogno che funzionino su un normale server http, ma l'url non può essere predeterminato. – Freewind