JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to get the current URL in JavaScript?

If you want to get the current URL, then call href property of the Location object. The URL returns by location.href property includes the following:

  • Protocol
  • Hostname or domain name
  • Port number
  • Query string parameters
  • Fragment identifier
let url = location.href;
console.log(url);
//https://www.tutorialsandyou.com/javascript/how-to-get-the-current-url-in-javascript-132.html

url = window.location.href;
console.log(url);
//https://www.tutorialsandyou.com/javascript/how-to-get-the-current-url-in-javascript-132.html

Recommended Posts