Objektet window.location
kan användas för att hämta den aktuella sidans adress (URL) och för att omdirigera webbläsaren till en ny sida.
Objektet window.location
kan skrivas utan fönsterprefixet.
Några exempel:
window.location.href
returnerar href (URL) för den aktuella sidan
window.location.hostname
returnerar webbhotellets domännamn
window.location.pathname
returnerar sökvägen och filnamnet för den aktuella sidan
window.location.protocol
returnerar webbprotokollet som används (http:eller https:)
window.location.assign()
läser in ett nytt dokument
Egenskapen window.location.href
returnerar webbadressen till den aktuella sidan.
Visa href (URL) för den aktuella sidan:
document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;
Resultatet är:
Prova själv →
<!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>
Egenskapen window.location.hostname
returnerar namnet på internetvärden (på den aktuella sidan).
Visa namnet på värden:
document.getElementById("demo").innerHTML =
"Page hostname is " + window.location.hostname;
Resultatet är:
Prova själv →
<!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>
Egenskapen window.location.pathname
returnerar sökvägen för den aktuella sidan.
Visa sökvägsnamnet för den aktuella webbadressen:
document.getElementById("demo").innerHTML =
"Page path is " + window.location.pathname;
Resultatet är:
Prova själv →
<!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>
Egenskapen window.location.protocol
returnerar webbprotokollet för sidan.
Visa webbprotokollet:
document.getElementById("demo").innerHTML =
"Page protocol is " + window.location.protocol;
Resultatet är:
Prova själv →
<!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>
Egenskapen window.location.port
returnerar numret på internetvärden port (på den aktuella sidan).
Visa namnet på värden:
document.getElementById("demo").innerHTML =
"Port
number is " + window.location.port;
Resultatet är:
Prova själv →
<!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>
De flesta webbläsare visar inte standardportnummer (80 för http och 443 för https)
Metoden window.location.assign()
läser in ett nytt dokument.
Ladda ett nytt 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>
Prova själv →
<!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>