Animations

API

Arrays

Async

Basics

Challenges

Classes

Console

Dates

Debugging

DOM Elements

DOM Methods

DOM Navigation

DOM Properties

Event Listeners

Flow Control

Forms

Functions

Global Functions

JSON

Keywords

Libraries (3rd party)

Math

Modules

Objects

Snippets

String

Types

Widgets

Window Object

JavaScript - Location

The window.location object contains many properties and methods and can be used to get the current page address (URL) and to redirect the browser to a new page.

The window.location object can be written without the window prefix. Some examples:

  • window.location.href returns the href (URL) of the current page
  • window.location.hostname returns the domain name of the web host (ip or url)
  • window.location.pathname returns the path and filename of the current page
  • window.location.protocol returns the web protocol used (http:// or https://)
  • window.location.assign(url) loads a new document (takes url)

let output = "";
let Location =
{
    Href: window.location.href,
    HostName: window.location.hostname,
    PathName: window.location.pathname,
    Protocol: window.location.protocol,
    Assign: window.location.assign
}

output += "href: " + Location.Href + "<br />";
output += "hostname: " + Location.HostName + "<br />";
output += "pathname: " + Location.PathName + "<br />";
output += "protocol: " + Location.Protocol + "<br />";
output += "assign: " + Location.Assign + "<br />";

document.getElementById("demo1").innerHTML = output;


Here is a simple example on how to use assign to send the user to another URL.


function GetOuttaHere()
{
    window.location.assign("http://www.google.com");
}

http://www.w3schools.com/js/js_window_location.asp