It is very easy to get current URL because JavaScript provides window.location object which has all-important URL parts of the current location.
Below are some data you can get from window.location object:
- hash - returns or sets the anchor part (#) of a URL,
- href - returns or sets the entire URL,
- host - returns or sets the hostname and port number of a URL,
- port - returns or sets the port number of a URL,
- hostname - returns or sets the hostname of a URL,
- origin - returns the protocol, hostname and port number of a URL,
- pathname - returns or sets the pathname of a URL,
- protocol - returns or sets the protocol of a URL,
- search - returns or sets the query string part of a URL.
Code:
console.log(window.location.href);
console.log(window.location.host);
console.log(window.location.origin);
console.log(window.location.port);
console.log(window.location.search);