In JavaScript, the DOM Location
object is part of the window object and contains information about the current URL. It can be accessed through the window.location
property.
Location
objecthash
It returns the anchor part of the current URL, including the #.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.hash);
// will return: #example
host
It returns the hostname, a :
(colon), and port number of the current URL.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.host);
// will return: educative.io:43
port
It returns the port number of the current URL.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.port);
// will return: 43
hostname
It returns the hostname of the current URL.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.hostname);
// will return: educative.io
protocol
It returns the protocol of the current URL.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.protocol);
// will return: https
origin
It returns the protocol of the current URL.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.origin);
// will return: https
pathname
It returns the pathname of the current URL.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.pathname);
// will return: /shot
search
It returns the query string part of the current URL.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.search);
// will return: ?q=location
href
It returns the current URL.
Assuming that the URL is https://educative.io:43/shot?q=location#example
.
console.log(location.href);
// will return: https://educative.io:43/shot?q=location#example
Location
objectassign()
It loads a new document at the URL provided in the parameter.
location.assign('https://shubhamkshatriya25.github.io/');
reload()
It reloads the URL, just like a refresh.
location.reload();
replace()
It replaces the current URL with a new URL provided in the parameter.
location.replace('https://shubhamkshatriya25.github.io/');