0

How to maintain scroll position when my page refresh by clicking hyperlink? I trying to do a hyperlink that will add quantity to an item and refresh the page. But everytime i click on the link i will get back to the top of page.

 <a href="selectquantity.php?remove='.$id.'" >[-]</a> 
Darcy
  • 63
  • 5
  • 13
  • 3
    Best improvement? Use ajax and don't refresh the whole page. Everything else is a fix with multiple but poor solutions. – Tyr May 03 '16 at 20:15
  • It is hard for my programming level. Because all my data is taken out from database. Can you just teach me the solution for my question? – Darcy May 03 '16 at 20:18
  • Best way for easy understanding on beginner level is using jQuery. It's easy to understand, you can use ajax calls without effort and you will have fast results. Try it out: https://jquery.com/ – Tyr May 03 '16 at 20:23

1 Answers1

2

If you want to refresh the page, you can use javascript or jquery to restore the scroll position.

For javascript, you can follow the guide from Restoring page scroll position with jQuery

The codes are as simple as:

var scroll = $(window).scrollTop();
$("html").scrollTop(scroll);

For jquery, you can follow the guide from how to remember scroll position of page

The codes are:

// When document is ready...
$(document).ready(function() {

// If cookie is set, scroll to the position saved in the cookie.
if ( $.cookie("scroll") !== null ) {
    $(document).scrollTop( $.cookie("scroll") );
}

// When a button is clicked...
$('#submit').on("click", function() {

    // Set a cookie that holds the scroll position.
    $.cookie("scroll", $(document).scrollTop() );

});//end of submit 

});//end of document.ready
Community
  • 1
  • 1
iCezz
  • 624
  • 1
  • 7
  • 14