JavaScript Window Location
The window.location
object is used to get the current page's 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:
location.hostname
returns the domain name of the web hostlocation.pathname
returns the path and file name of the current pagelocation.port
returns the port of the web host (80 or 443)location.protocol
returns the web protocol used (http: or https:)
Window Location href
The location.href
property returns the URL of the current page.
Example
Returns the entire URL of the current page:
<script>
document.write(location.href);
</script>
Window Location pathname
The location.pathname
property returns the pathname of the URL.
Example
Returns the pathname of the current URL:
<script>
document.write(location.pathname);
</script>
Window Location assign
The location.assign()
method loads a new document.
Example
Loads a new document:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
<script>
function newDoc(){
window.location.assign("https://www.tutorialpro.org")
}
</script>
</head>
<body>
<input type="button" value="Load new document" onclick="newDoc()">
</body>
</html>