Javascript Location - 4 Methods and 10 Properties (Reference)
The javascript location object is used to access the location of the document. It returns a location object which has 4 methods and 10 properties. The location object is also used to change the location of the document.
Location Methods:
assign()
Loads the resource at the URL provided in parameter.
window.location.assign("NEW_URL");
reload()
Reloads the current URL, like the Refresh button.
window.location.reload();
replace()
Replaces the current resource with the one at the provided URL.
window.location.replace("NEW_URL");
toString()
Returns a USVString containing the whole URL.
var x = document.getElementById("a_TAG");
var y = x.toString();
ancestorOrigins
Is a static DOMStringList containing, in reverse order, the origins of all ancestor browsing contexts of the document associated with the given Location object.
var x = window.location.ancestorOrigins;
// DOMStringList {length: 0}
href
Is a stringifier that returns a USVString containing the entire URL. If changed, the associated document navigates to the new page.
var x = window.location.href;
// 'https://www.webdevfix.com/javascript-location-4-methods-and-10-properties-reference'
protocol
Is a USVString containing the protocol scheme of the URL, including the final ':'.
var x = window.location.protocol;
// 'https:'
host
Is a USVString containing the host, that is the hostname, a ':', and the port of the URL.
var x = window.location.host;
hostname
Is a USVString containing the domain of the URL.
var x = window.location.hostname;
port
Is a USVString containing the port number of the URL.
var x = window.location.port;
pathname
Is a USVString containing an initial '/' followed by the path of the URL.
var x = window.location.pathname;
// "/javascript-location-4-methods-and-10-properties-reference"
search
Is a USVString containing a '?' followed by the parameters or "querystring" of the URL.
var x = window.location.search;
var x = URL.searchParams;
// "?SEARCH_PARAMETERS"
hash
Is a USVString containing a '#' followed by the fragment identifier of the URL.
var x = window.location.hash;
// "#HASH_IDENTIFIER"
origin
Returns a USVString containing the canonical form of the origin of the specific location.
var x = window.location.origin;
// 'https://www.webdevfix.com'
// "file://"
