JavaScript Window Location, javascriptlocation
The window. location object is used to obtain the address (URL) of the current page and redirect the browser to the new page.
Window Location
Window. locationObjects can be written without the window prefix. Some examples:
Some instances:
- Location. hostname: The domain name of the web host.
- Location. pathname: return the path and file name of the current page.
- Location. port returns the port (80 or 443) of the web host)
- Location. protocol returns the web protocol used (https: // or https ://)
Window Location Href
The location. href attribute returns the URL of the current page.
Instance
Return the whole URL (the current page:
<Script>
Document. write (location. href );
</Script>
The above code is output:
Https://www.runoob.com/js/js-window-location.html
Window Location Pathname
The location. pathname attribute returns the URL path name.
Instance
Returns the path name of the current URL:
<Script>
Document. write (location. pathname );
</Script>
The above code is output:
/Js/js-window-location.html
Window Location Assign
The location. assign () method loads a new document.
Instance
Load a new document:
<Html>
<Head>
<Script>
Function newDoc ()
{
Window. location. assign ("https://www.w3cschool.cc ")
}
</Script>
</Head>
<Body>
<Input type = "button" value = "Load new document" onclick = "newDoc ()">
</Body>
</Html>