window.location object to get the current URL of the client’s web page.
Window Location Href Property
window.location.href property of
window.location object can return the current web page URL where a visitor is staying. We are going to see an example.
Our current page URL will be shown in
p tag which has an ID that you can see below:
In the above code, we have put the current page URL in a variable
currentURL. After that, we have selected our paragraph tag by its ID and set the HTML content with the
innerHTML property. In our HTML content, we pass the variable that contains the page URL.
Now we can run our code.
Let’s try it yourself, run the code on your browser. If you run the code, you will be able to see the current page URL on the web page.