JavaScript Location
In this tutorial, you will learn about the JavaScript Location object and how to manipulate the location effectively.
The Location object represents the current location (URL) of a document. You can access the Location object by referencing the location property of the window or document object. Both window.location and document.location link to the same Location object.
The location object is accessed with:
window.location or just location
let origin = window.location.origin; or let origin = location.origin;
Location Object Properties
hash - Sets or returns the anchor part (#) of a URL
host - Sets or returns the hostname and port number of a URL
hostname - Sets or returns the hostname of a URL
href - Sets or returns the entire URL
origin - Returns the protocol, hostname and port number of a URL
pathname - Sets or returns the path name of a URL
port - Sets or returns the port number of a URL
protocol - Sets or returns the protocol of a URL
search - Sets or returns the querystring part of a URL
Location Object Methods
assign() - Loads a new document Try It
reload() - Reloads the current document Try It
replace() - Replaces the current document with a new one Try It