Search code examples
javascripthtmlrefreshhrefreload

How do I refresh a page when clicking on a href? | HTML, CSS


What do I have:

HTML, CSS, JS

What do I need:

Reload a page when clicking on <a href>.

What do I know:

I can reload a page using window.location.reload(). To use it in HTML code, I have to put it into <script> tags.

What don't I know:

How to execute this script code when clicking on <a href>.

My code:

<ul>
    ....
    <a href="#" class="currentpage"><li>About us</li></a>
</ul>



What have I tried:

Nothing yet. I have no idea how to link the <script> with <a href>.


Solution

  • If you want to reload the current page you can use this code:

    <a href="#" onclick="myFunction()" class="currentpage"><li>About us</li></a>
    
    <script>
    function myFunction() {
      window.location.reload();
    }
    </script>

    It will execute the given function after you click on the link.