Antworten:
var host = window.location.hostname;
oder möglicherweise
var host = "http://"+window.location.hostname;
oder wenn Sie Verkettung mögen
var protocol = location.protocol;
var slashes = protocol.concat("//");
var host = slashes.concat(window.location.hostname);
http. Verwenden Sie das relative Protokoll. Könnte geeigneter sein als eine hartcodierte.
concat. Im Beispiel var a = 1 + 2 + " should be 12";gegen die Concat-Version davon var a = "".concat(1).concat(2).concat(" should be 12");. Die Verwendung von concat erspart Ihnen viel Ärger +bei der Berechnung und nicht bei der Verkettung.
hostnamegibt nur Domain und hostliefert auch Port. Dies ist ein großartiges Mini-Tool, um den Link Anatomie zu sehen bl.ocks.org/abernier/3070589
So erhalten Sie den Hostnamen: location.hostname
Ihr Beispiel sucht jedoch auch nach dem Schema, location.originscheint also in Chrome das zu tun, was Sie wollen, wird jedoch in den Mozdev-Dokumenten nicht erwähnt. Sie können es mit konstruieren
location.protocol + '//' + location.hostname
Wenn Sie auch die Portnummer möchten (wenn es nicht 80 ist), dann:
location.protocol + '//' + location.host
Sie können das Protokoll, den Host und den Port folgendermaßen abrufen:
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) |
Die gesamte Browserkompatibilität erfolgt über das Mozilla Developer Network
let path = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
Das sollte funktionieren:
window.location.hostname
hostwenn Sie auch Hafen brauchen
Abhängig von Ihren Anforderungen können Sie eine der window.locationEigenschaften verwenden. In Ihrer Frage fragen Sie nach dem Host , der mit window.location.hostname(z www.example.com. B. ) abgerufen werden kann . In Ihrem Beispiel zeigen Sie etwas, was als Ursprung bezeichnet wird und mit window.location.origin(z http://www.example.com. B. ) abgerufen werden kann .
var path = window.location.origin + "/";
//result = "http://localhost:60470/"
Ich mag dieses je nach Zweck
window.location.href.split("/")[2] == "localhost:17000" //always domain + port
Sie können es auf jede URL-Zeichenfolge anwenden
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"
Entfernen von Protokoll, Domäne und Pfad aus der URL-Zeichenfolge (relativer Pfad)
var arr = url.split("/");
if (arr.length>3)
"/" + arr.splice(3, arr.length).join("/") == "/sub1/sub2/mypage.html?q=12"