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
<title>Get current page address using JavaScript</title>

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

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

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

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.

Try Out Code2care Dev Tools:


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, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.