Aziz
Aziz

Reputation: 7783

Force HTML Internal Links / Fragment identifier (#) to reload page

How can I make internal links reload the page as if it was another HTTP request?

Consider the following basic example:

#content {margin-top:1400px;}
<a href="#content">Go to Content</a>

<section id="content">Content</section>

The Go to Content anchor will scroll to target section and add the hash #content to URL (example.com/#content). How would I force this to reload/refesh the page as if it was an external link like example.com/content/

Would accept server-side or JavaScript solutions.

Upvotes: 0

Views: 849

Answers (2)

Brijal Savaliya
Brijal Savaliya

Reputation: 1091

Use this

    <script>
function gototab(reload)
   {
    window.location.href = 'test.php#content';
    window.location.reload(true);
   }
</script>

<a href="#content" onclick="gototab();">Go to Content</a>

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074385

You could add a click handler to links that schedules a location.reload() to happen after the event is finished.

Here's one using delegated handling:

document.body.addEventListener("click", function(e) {
    var node = e.target;
    while (node && node != this) {
        if (node.nodeName.toUpperCase() == "A") {
            setTimeout(function() {
                location.reload();
            }, 0);
            return;
        }
        node = node.parentNode;
    }
}, false);

You'll probably want to filter that a bit.

Upvotes: 2

Related Questions