Position des JavaScript-Fensters


Inhaltsverzeichnis

    Inhaltsverzeichnis anzeigen


Zum Abrufen kann das Objekt window.location verwendet werden die aktuelle Seitenadresse (URL) und um den Browser auf eine neue Seite umzuleiten.


Fensterposition

Das window.location-Objekt kann ohne das Fensterpräfix geschrieben werden.

Einige Beispiele:

  • window.location.href gibt die href (URL) der aktuellen Seite zurück

  • window.location.hostname gibt den Domänennamen des Webhosts zurück

  • window.location.pathname gibt den Pfad und Dateinamen der aktuellen Seite zurück

  • window.location.protocol gibt das verwendete Webprotokoll zurück (http:oder https:)

  • window.location.assign() lädt ein neues Dokument


Fensterposition Href

Die Eigenschaft window.location.href gibt die URL der aktuellen Seite zurück.

Beispiel

Zeigt die href (URL) der aktuellen Seite an:

document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;

Ergebnis ist:

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript</h2>

<h3>The window.location object</h3>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 
"The full URL of this page is:<br>" + window.location.href;
</script>

</body>
</html>

Hostname des Fensterstandorts

Die Eigenschaft window.location.hostname gibt den Namen des Internet-Hosts (der aktuellen Seite) zurück.

Beispiel

Den Namen des Hosts anzeigen:

document.getElementById("demo").innerHTML =
"Page hostname is " + window.location.hostname;

Ergebnis ist:

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript</h2>

<h3>The window.location object</h3>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 
"Page hostname is: " + window.location.hostname;
</script>

</body>
</html>


Pfadname für den Fensterstandort

Die Eigenschaft window.location.pathname gibt den Pfadnamen von zurück die aktuelle Seite.

Beispiel

Den Pfadnamen der aktuellen URL anzeigen:

 document.getElementById("demo").innerHTML =
"Page path is " + window.location.pathname;

Ergebnis ist:

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript</h2>

<h3>The window.location object</h3>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
"Page path is: " + window.location.pathname;
</script>

</body>
</html>

Fensterstandortprotokoll

Die Eigenschaft window.location.protocol gibt das Webprotokoll der Seite zurück.

Beispiel

Webprotokoll anzeigen:

document.getElementById("demo").innerHTML =
"Page protocol is " + window.location.protocol;

Ergebnis ist:

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript</h2>

<h3>The window.location object</h3>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
"The page protocol is: " + window.location.protocol;
</script>

</body>
</html>

Fensterstandort-Port

Die Eigenschaft window.location.port gibt die Nummer des Internet-Hosts zurück Port (der aktuellen Seite).

Beispiel

Den Namen des Hosts anzeigen:

document.getElementById("demo").innerHTML =
"Port 
  number is " + window.location.port;

Ergebnis ist:

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript</h2>

<h3>The window.location object</h3>

<p id="demo"></p>

<p><b>Note: </b>If the port number is default (80 for http and 443 for https), most browsers will display 0 or nothing.</p>

<script>
document.getElementById("demo").innerHTML = 
"The URL port number of the current page is: " + window.location.port;
</script>

</body>
</html>

Die meisten Browser zeigen keine Standard-Portnummern an (80 für http und 443 für https).


Fensterstandort zuweisen

Die Methode window.location.assign() lädt ein neues Dokument.

Beispiel

Laden Sie ein neues Dokument:

<html>
<head>
<script>
function newDoc() {
  window.location.assign("https://www.w3schools.com")
 }
</script>
</head>
<body>

<input type="button" value="Load new document"
onclick="newDoc()">

</body>
</html>

Probieren Sie es selbst aus →

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript</h2>

<h3>The window.location object</h3>

<input type="button" value="Load new document" onclick="newDoc()">

<script>
function newDoc() {
  window.location.assign("https://www.w3schools.com")
}
</script>

</body>
</html>