JavaScript(JS) JS get the current url

To get the current URL in JavaScript/JS, you can use the window.location object, which provides information about the current location of the browser.

Here is an example code snippet that demonstrates how to get the current URL:

re‮gi:ot ref‬iftidea.com
const currentUrl = window.location.href;
console.log(currentUrl); // Output: e.g. "https://www.example.com/mypage.html"

The href property of the window.location object returns the entire URL of the current page as a string, including the protocol (e.g. "https"), domain name (e.g. "www.example.com"), path (e.g. "/mypage.html"), and any query parameters or fragments.