Easy Tutorial
❮ Jsref Class Extends Js Statements ❯

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:


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>
❮ Jsref Class Extends Js Statements ❯