Scrolling with anchor

0

I need to make an anchor on my website and at the moment I have the following script :

<script>
  $(document).ready(function() { 
     window.location.href='#foo';
  });
</script>

It directs the page to the desired element, but by clicking the page it all goes down to where I want it with the tag:

<a href="#" id="foo"></a>

On this page I have links to photo galleries. I click, I'm redirected to the galleries normally and everything works as it should. But on the gallery page I have a back button that, when clicking, should go back to the previous page:

echo "<a href=\"about.php\" id=\"foo\" class=\"back\"><img src=\"images/left.png\"/> Voltar</a><br />";

It turns out that this way the anchor is not maintained. When I click back, the previous page is displayed, however, on top of it, not the anchor I was before entering the gallery.

How can I make the anchor hold, and when I click back, return to the same position as the page I was in?

    
asked by anonymous 22.02.2017 / 12:44

1 answer

1

If the intention is to just go back to the previous page, I believe that using JavaScript to access browser history is an option, using the history.back() function.

window.history.back()

Instead of the link printed by PHP, put a button with the action onclick :

<button type="button" onclick="window.history.back()">
  Voltar
</button>

In this way, if the user came from the /index.php#foo page, doing the history back will keep the URL fragment.

    
22.02.2017 / 13:16