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.




Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.