JavaScript Window Location
The window.location object is used to get the address (URL) of the current page and redirect the browser to a new page.
Window Location
window.location The object can be used without the window prefix when writing. Some examples:
Some examples:
- location.hostname returns the domain name of the web host
- location.pathname returns the path and filename of the current page
- location.port returns the port of the web host (80 or 443)
- location.protocol returns the web protocol used (http: or https:)
Window Location href
location.href property returns the URL of the current page.
Example
Return the entire URL (of the current page):
The above code outputs:
Window Location pathname
location.pathname property returns the pathname of the URL.
Example
Return the pathname of the current URL:
The above code outputs:
Window Location assign
location.assign() method loads a new document.
Example
Load a new document:
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>Runoob Tutorial(runoob.com)
title
<
script
function newDoc(){ window.location.assign("https://www.runoob.com") }
script
head
<body>
<input type="button" value=">Load new document
"
onclick
=
"
newDoc()
"
body
html
Try it »