On clicking a link, it should redirect to the current page. How do I do this with JavaScript?
In JavaScript, page redirection is simple. The window object's location object is a property. A web page can be redirected in a number of ways.
You need to add ” id attribute” to the section you want to show and use the same id in href attribute with “#” in the anchor tag. So that On click a particular link, you will be redirected to the section that has same id mention in anchor tag.
Assuming you mean the link should refresh the current page, you can use window.location.reload()
. In jQuery it would look like this:
<a href="#" id="myLink">Refresh current page</a>
$("#myLink").click(function() { window.location.reload(); });
In plain JS it would look like this:
document.querySelector("#myLink").addEventListener('click', function() { window.location.reload(); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With