Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Window Location

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:


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 »