ja***@cyberpine.com wrote:
Hi,
I'm working with ASP.NET serverside and am looking for an easy way to
retain scroll position of the window.
Is there any way that a hidden form field could *always* have the
current document.scrolltop value set to it? Especially right before any
posting to the server is done.
To always have it is different than to have it when the post is done:-)
If you only require it with the posting, then check the following.
---
<style type="text/css">
#foo {
width: 1500px;
height: 1500px;
position:absolute;
padding:50% 0 0 50%;
}
</style>
<div id="foo">
<form action="foo" onsubmit="return primaryHandler(event)">
<input type="text" name="scrollPos">
<input type="submit">
</form>
</div>
<script type="text/javascript">
(function(){
function getCompatModeElement(){
var func;
if(
document.compatMode &&
document.compatMode.indexOf("CSS")!=-1 &&
document.documentElement
){
func=function(){return document.documentElement;}
} else if(document.body) {
func=function(){return document.body;}
} else {
func=function(){return null;}
}
return (getCompatModeElement=func)();
}
function getScrollPos(){
var func;
if(typeof window.pageYOffset=="number") {
func=function(){
return {x:window.pageXOffset, y:window.pageYOffset};
}
} else {
var c=getCompatModeElement();
if(c){
func=function(){
return {x:c.scrollLeft, y:c.scrollTop};
}
} else {
func=function(){
return {x:-1, y:-1};
}
}
}
return (getScrollPos=func)();
}
function saveScrollPos(frm){
var p=getScrollPos();
frm.elements["scrollPos"].value=p.x+","+p.y;
return true;
}
// define the handler
var frm=document.forms[0];
frm.onsubmit=(function(p){
return function(evt){
return saveScrollPos(this) && p ? p(evt) : true;
}
})(frm.onsubmit);
})();
// normal handler
function primaryHandler(evt){return false; /*anything*/}
</script>
---
HTH,
Yep.