views:

530

answers:

3

After I submit a form the position will return to the top of the window. Instead of going to the previous position.

Looking at using scrolltop to rememeber the positon or do you have a better idea?

I'm using stuff like PHP5, jQuery and MySQL.

A: 

If you submit you Form via Ajax it won't change anything (maybe that is an option, if you redirect to the same page anyway). You can also save the position in a Cookie via JavaScript before submitting and retrieve and set the value when the new page is loaded (e.g. with jQuery scrollTo). Or you just set an anchor on your Form field (which is very likely to be the last position the user scrolled to) and scroll back to the form after submit.

Daff
+1  A: 

First create an anchor in your page where you want the visitor to get to when they submit the form.

Then in your form action or redirect point to file with the anchor

e.g.

 <div id="view_from_here" >.....

then

 <form action="myfile.php#view_from_here" ....
Derek Organ
+1  A: 

Check this out:

<script type="text/javascript" src="der/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="der/jquery.cookie.js"></script>
<script type="text/javascript">
function get_position(){
 var top_position = document.documentElement.scrollTop;
 $.cookie('pos', top_position);
};
function set_position(){
 var top_position = $.cookie('pos');
 window.scrollTo(0,top_position)
 $.cookie('pos', null);
}; 
</script>
</head>
<body onload="set_position();">
.
.
.
<form method="post" action="" onsubmit="return get_position();">
</form>
Cudos