Link with Anchor

1

I'm making a website and I have a link that, in addition to sending a message when it's clicked, uses an anchor to put the featured section of the site.

I'm doing this as follows:

<a href="/alerta#destaque">Gerar Alerta</a>

In this way, when the user clicks on the link, an alert is generated and the page "descends" to a div with the "highlight" id that shows the contents of the generated alert.

The alert is generated from a JQuery function.

My problem is that this link works only once. The first time I click, everything happens perfectly. But if I click on the link again nothing happens.

I realized that if the anchor link is visible in the URL the link action does not work.

How could I solve this problem?

    
asked by anonymous 30.04.2018 / 07:15

1 answer

0

You can restore the URL of the page (by removing alerta#destaque ) without being reloaded using the history.replaceState method.

For example, from site.com.br/alerta#destaque to site.com.br .

In your jQuery function quoted in the question, put the code at the end:

history.replaceState("", null, window.location.pathname);

The window.location.pathname returns only the current page name, ignoring parameters, hash, etc. Ex.:

site.com.br/alerta#destaque returns /

site.com.br/index.php?alerta#destaque returns index.php

    
30.04.2018 / 14:10