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.hostnamereturns the domain name of the web hostlocation.pathnamereturns the path and file name of the current pagelocation.portreturns the port of the web host (80 or 443)location.protocolreturns 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>