JavaScript : Get current page address


Sometimes as a developer we need to know the address (URL) of the current page in order to generate some page links using javaScript, in oder to get the address of the current page we can make use of window.location object,

File : getAddress.html
<html>
<head>
<title>Get current page address using JavaScript</title>
</head>
<body>

<div id="pageDetails"></div>

<input type="button" value="Click to get Address" onclick="getCurrentPageAddress()">


<script>
function getCurrentPageAddress() {
    var currPageURL = window.location;
    document.getElementById("pageDetails").innerHTML = "Current Page Address : " + currPageURL;
}
</script>

</body>
</html>
Get Page Address using JavaScript
Get Page Address using JavaScript

We have a webpage with a button when clicked the address of the current page is displayed within the div element with id pageDetails.

Note: window.location object is also used to redirect a page to another.



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap