How to maintain screen positioning after postback?

Asked

Viewed 914 times

0

I have a very extensive html form with several entries , after making the postback my page is redirected to the top of the form, postback return to the page I left ?

1 answer

1

You need to save the value of window.pageYOffset (document.documentElement.scrollTop to IE8) and restore as soon as your page loads again.

If you are using jQuery you can do so:

  1. Add an Hidden field to store scroll value
<form id="form1">
    ....
    <input type="hidden" name="scrollto" id="scrollto" value="0" />
</form>
  1. The code below saves the value of $.scrollTop in scrollto at the time the form is sent and already restores the scroll position when the page is loaded.
$(function(){
  $('#form1').submit(function(){ 
     $('#scrollto').val( $(window).scrollTop() ); 
  });

  $(window).scrollTop( $('#scrollto').val() ); 
});
  • It didn’t work; My form contains 3 types of registration :Address,contact,vehicle, on the same page ;each form is separated in Divs ,each DIV is part of my javascript function: <script> $(Function () { $("#accordion").accordion(); }); </script> this function splits the form and puts it in Tabs! Separate modules, visually improved a lot but, when I do the Postback it does not continue from the ABA where I stopped , it is redirected to the 1st ABA! What I do????

  • All this in one form?

  • Not every TAB ,Each Form has its form.

  • Then all you have to do is place the comma-separated Forms ids on the selector ($('#form1, #form2,....)'), change the id="scrollto" for class="scrollto" and change the selector #scrollto for .scrollto in the code. Remembering that you should add the input to all forms.

Browser other questions tagged

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