Агуулга
The
window.location
object can be used to get the current page address (URL) and to redirect the browser to a new page.
Window Location #
The window.location
object can be written without the window prefix.
Some examples:
window.location.href
returns the href (URL) of the current pagewindow.location.hostname
returns the domain name of the web hostwindow.location.pathname
returns the path and filename of the current pagewindow.location.protocol
returns the web protocol used (http: or https:)window.location.assign()
loads a new document
Window Location Href #
The window.location.href
property returns the URL of the current page.
Example #
Display the href (URL) of the current page:
document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;
Result is:
Page location is https://www.Apprentice.mn/js/js_window_location.asp
document.getElementById("demo").innerHTML =
"Page hostname is " + window.location.hostname;
Result is:
Page hostname is www.Apprentice.mn
document.getElementById("demo").innerHTML =
"Page path is " + window.location.pathname;
Result is:
Page path is /js/js_window_location.asp
document.getElementById("demo").innerHTML =
"Page protocol is " + window.location.protocol;
Result is:
Page protocol is https:
document.getElementById("demo").innerHTML =
"Port number is " + window.location.port;
Result is:
Port number is
<html>
<head>
<script>
function newDoc() {
window.location.assign("https://www.Apprentice.mn")
}
</script>
</head>
<body>
<input type="button" value="Load new document" onclick="newDoc()">
</body>
</html>