xxxxxxxxxx
const currentURL = window.location.href;
console.log(currentURL);
xxxxxxxxxx
document.URL
> "http://example.com/page1.html"
document.location.href
> "http://example.com/page1.html"
document.location.pathname
> "/page1.html"
document.location.origin
> "http://example.com"
xxxxxxxxxx
window.location.hash: "#2"
window.location.host: "localhost:4200"
window.location.hostname: "localhost"
window.location.href: "http://localhost:4200/landing?query=1#2"
window.location.origin: "http://localhost:4200"
window.location.pathname: "/landing"
window.location.port: "4200"
window.location.protocol: "http:"
window.location.search: "?query=1"
xxxxxxxxxx
window.location.pathname; // Returns path only (/path/example.html)
window.location.href; // Returns full URL (https://example.com/path/example.html)
window.location.origin; // Returns base URL (https://example.com)
xxxxxxxxxx
// Get url/path/baseUrl following.
window.location.pathname; // Returns path only (/path/example.html)
window.location.href; // Returns full URL (https://example.com/path/example.html)
window.location.origin; // Returns base URL (https://example.com)
xxxxxxxxxx
function getURL() {
alert("The URL of this page is: " + window.location.href);
}
xxxxxxxxxx
// Get the current URL
var currentUrl = window.location.href;
// Display the URL in the console
console.log(currentUrl);
xxxxxxxxxx
console.log(window.location.href);
As noted in the comments, the line below works, but it is bugged for Firefox.
document.URL