Risposte:
var host = window.location.hostname;
o forse
var host = "http://"+window.location.hostname;
o se ti piace la concatenazione
var protocol = location.protocol;
var slashes = protocol.concat("//");
var host = slashes.concat(window.location.hostname);
httpperò. Utilizzare il protocollo relativo. Potrebbe essere più appropriato di quello hard-coding.
concat. Nell'esempio var a = 1 + 2 + " should be 12";rispetto alla versione concat di questo var a = "".concat(1).concat(2).concat(" should be 12");. L'uso di concat ti farà risparmiare un sacco di problemi +per il calcolo, non per la concatenazione.
hostnamefornirà solo il dominio e hostfornirà anche la porta. Questo è un mini strumento fantastico per vedere l'anatomia dei collegamenti bl.ocks.org/abernier/3070589
Per ottenere il nome host: location.hostname
Ma anche il tuo esempio sta cercando lo schema, quindi location.originsembra fare quello che vuoi in Chrome, ma non viene menzionato nei documenti di Mozdev. Puoi costruirlo con
location.protocol + '//' + location.hostname
Se vuoi anche il numero di porta (per quando non è 80), allora:
location.protocol + '//' + location.host
È possibile ottenere il protocollo, l'host e la porta usando questo:
window.location.origin
| Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|----------------------------------|-------|-----------------|-------------------|-------|--------------------------------------------|
| (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| 30.0.1599.101 (possibly earlier) | ? | 21.0 (21.0) | 11 | ? | 7 (possibly earlier, see webkit bug 46558) |
| Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|----------------------------------|-------|------------------------|----------|--------------|--------------------------------------------|
| (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| 30.0.1599.101 (possibly earlier) | ? | 21.0 (21.0) | ? | ? | 7 (possibly earlier, see webkit bug 46558) |
Tutta la compatibilità del browser proviene da Mozilla Developer Network
let path = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
Questo dovrebbe funzionare:
window.location.hostname
hostse hai bisogno anche di un porto
A seconda delle esigenze, è possibile utilizzare una delle window.locationproprietà. Nella tua domanda stai ponendo domande sull'host , che può essere recuperato usando window.location.hostname(es www.example.com.). Nel tuo esempio stai mostrando qualcosa che si chiama origine , che può essere recuperato usando window.location.origin(es http://www.example.com.).
var path = window.location.origin + "/";
//result = "http://localhost:60470/"
Mi piace questo a seconda dello scopo
window.location.href.split("/")[2] == "localhost:17000" //always domain + port
Puoi applicarlo su qualsiasi stringa URL
var url = "http://localhost:17000/sub1/sub2/mypage.html?q=12";
url.split("/")[2] == "localhost:17000"
url.split("/")[url.split("/").length-1] == "mypage.html?q=12"
Rimozione di protocollo, dominio e percorso da url-string (percorso relativo)
var arr = url.split("/");
if (arr.length>3)
"/" + arr.splice(3, arr.length).join("/") == "/sub1/sub2/mypage.html?q=12"