Anchor scrolling

Asked

Viewed 356 times

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 when clicking on the page throughout it descends straight to where I want with the tag:

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

On this page I have links to photo galleries. I click, I am redirected to the galleries normally and everything works as it should. But on the gallery page I have a button come back which, when clicking, should return 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, at the top of it, not at the anchor I was before entering the gallery.

How can I get the anchor to hold and when I click back, go back to the same position as the page I was on?

1 answer

1

If the intention is just to return to the previous page, I believe that using Javascript to access your browser history is an option, through the function history.back().

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>

This way if user came from page /index.php#foo, by doing the history back, the fragment of the URL will be kept.

  • but I want it to leave the index2.php and go to index.php#foo, just by clicking the buttonback it activates the js and activates the #foo function, because the page does not go to the #foo function if not every time the user enters will go down to the gallery directly

  • So. My solution does not solve your problem?

  • if the function only goes back to the previous page with the msm link it came from, it will not work, but thanks for commenting on this function, I did not know.

  • Explain your problem better then. The user is on index.php#foo, click to enter the gallery and go to index2.php. In the index2.php there must be the back button that goes to index.php#foo. Isn’t that?

  • I have the gallery at index.php, but the gallery is at the bottom of the page, when it clicks on the gallery, redirects to index2.php only with the photos of this gallery, and I have a href called back, which goes back to the index.php page, and I need that when it clicks back, it returns with #foo function

  • Okay. My answer does just that. Why wouldn’t it work?

  • according to you it goes back to the page the user came from, it will come from the index.php page, to index2.php, and when I click back, it will go back to index.php, because I didn’t put the #foo function on the whole index.php page, or I

  • It goes back to the previous page, regardless of what it is, but I think I get your point. In case, the user will be in index.php (without #foo), He goes into the gallery, and when he comes back, he has to go back to index.php#foo, correct?

  • Yeah, I guess I just got caught up in explaining, that’s exactly it.

  • echo "<a href="candy.php#foo" class="back"><img src="images/left.png"/> Back</a><br />"; I’ve put this code now, it comes back with the foo function, but in my site, when I click on the tab to access the page, it redirects directly to the gallery, regardless of where I came from, I only need to get this problem, when clicking to access the page, it descend to the gallery, ( activate only the foo when back ad own gallery)

  • How are you displaying the gallery link? Is it with PHP too? If so, add the snippet of code in the question that will be easier to give an example.

  • <?php require_once 'gallery3.php'; ? > , I call the gallery by this link inside a box in the main php

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.