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

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!