sto chiamando l'URL base tramite javascript in una pagina html e voglio aggiungere l'URL di base per chiamare un linkgetBaseURL() funzione JavaScript chiamata a href = ""
ad es.
<a href="getBaseURL()/filepath/index.html">Page</a>
Il javascript è il seguente:
function getBaseURL() {
var url = location.href; // entire url including querystring - also: window.location.href;
var baseURL = url.substring(0, url.indexOf('/', 14));
if (baseURL.indexOf('http://localhost') != -1) {
// Base Url for localhost
var url = location.href; // window.location.href;
var pathname = location.pathname; // window.location.pathname;
var index1 = url.indexOf(pathname);
var index2 = url.indexOf("/", index1 + 1);
var baseLocalUrl = url.substr(0, index2);
return baseLocalUrl + "/";
}
else {
// Root Url for domain name
return baseURL + "/";
}
}
EDIT
Il suo solo per ottenere qualsiasi URL di base del dominio in questione. Inoltre, ho ottenuto questo codice da un tutorial in modo tutti i suggerimenti che richiedono meno elaborazione sarebbe grande grazie
SECONDO EDIT
Ciao a tutti grazie per le vostre risposte finora, però, sto cercando di chiamare il funzione per il tag html come in <a href="getURL()/filepath/file.html></a>
. Questo è dove sta il problema, non sono sicuro come fare
Hai una domanda? Btw, 'localhost0' e' localhost.foo.com' sono domini perfettamente validi e non quelli che sono tipicamente trattati specialmente in/etc/hosts o altre configurazioni di risoluzione dei nomi, quindi il tuo "" http: // localhost "=== baseUrl .substring (0, 14) 'ha un sacco di ipotesi nascoste. Stai anche ignorando l'effetto di un ' '. –
Puoi fornire un URL di esempio e che cosa potrebbe essere "baseURL"? Ho la sensazione che tu faccia un sacco di elaborazione non necessaria ... –
per favore definisci "baseURL" –