0

this is my link

echo '<a  href="editlist.php?whatspacked=' . $idandcat . '&print=' . $tablename .'&scroll='. $idplace . '">';  

the links is to the same page

I what to save the scroll position (Y)

1.how do send the position inside My link? ($idplace)

2.how do I set the position after getting it ?

tried to play with

<script>

window.onload = function () {

    var scrollTop1 = document.getElementById('scroll').value;

    document.documentElement.scrollTop = document.body.scrollTop = scrollTop1;
}

but can't get the scroll value

Thanks

Amit
  • 3
  • 3

1 Answers1

0

You can use hrefs to scroll to an object with id of something like

<a href="#top">Top</a>

would scroll to whatever has id="top"

See this link for more information https://www.computerhope.com/issues/ch000049.htm

I think this would probably solve your problem but if you still want to be able to get the offset of your element look into this issue Get item offset from the top of page

  • I don't want to scroll to an object, I want to save scroll position . I need to send info to page – Amit Oct 01 '18 at 09:38
  • Hiya, did you take a look at the second link I posted, this should help you get the offset to an object on the page and then you can use that information in javascript however needed. Like so: $y = $elem.getBoundingClientRect().top + window.scrollY; where $elem is whatever object you want to measure from – Eleonora Ivanova Oct 01 '18 at 09:51
  • still dosnt help. I need to send the screen position . see my code – Amit Oct 01 '18 at 11:06
  • The $idplace is a php var in your template, that gets processed and replaced with whatever the value is and printed to the dom before any of your js has ran. If the value is something you need to get using javascript after page load, you don't need that in your php template. However keep in mind that if for some reason js is disabled or the version you are using is not supported this would break your link so thats something you want to catch in php when processing this request. – Eleonora Ivanova Oct 01 '18 at 11:44
  • thanks, is there a why to send the Y scroll position with out sending it in the href ? – Amit Oct 01 '18 at 12:15
  • Depends what are you trying to achieve, what is your reason for needing to send this when clicking the link? – Eleonora Ivanova Oct 01 '18 at 13:15
  • I have a list of items that I what the user to click if whats them. if he clicks on the item I send the page to it self to update sql table and update info of number of items . I would like the user to get back to the same position a – Amit Oct 01 '18 at 13:22
  • Right okay, you don't need to be passing the y position at all, if you use an ajax call on click of your element your page wont reload and the user position would not be interrupted on the screen I believe. If your page reloads anyway you don't want to be passing the offset as a number you want to tell the page to scroll to a particular element with an id of something. So either of this two options would be a more reliable than your initial approach. – Eleonora Ivanova Oct 01 '18 at 13:31
  • Thanks, I can see that `scrollTop = window.scrollY;` gives me the Y position, why cant I use it ? isn't there a way to send that var and use it after reloading the page ? – Amit Oct 01 '18 at 13:54
  • "whatspacked" is my item-id how can I send the scroll to that id ? – Amit Oct 01 '18 at 14:03
  • You can as we discussed earlier, however, what if something on your page fails to load the second time or the browser resizes. The offset calculated wont be accurate anymore, if you don't want to disrupt the user scrolling you should use an ajax call to post to your route and let the user continue on scrolling – Eleonora Ivanova Oct 01 '18 at 14:19
  • ok, Thanks. will try it again. But still looking for a simple code that will keep scroll position after reloading page – Amit Oct 01 '18 at 14:27
  • Right, okay if you truly want to do that, you can save it in the browser session, but I would again advice to scroll to an id of an element rather than the offset because that might not be the same place if something on the page is different. See this for reference https://stackoverflow.com/questions/34261365/retain-scrollbar-position-even-after-reloading-using-javascript. In production code you would just use ajax if you want not to disrupt users expirience with a page reload – Eleonora Ivanova Oct 01 '18 at 15:25