storing scroll position in hdden field

J

jason

Hello.

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.

I'm using asp.net datagrid code that does not give me any easy way to
tie javascript functions to controls - otherwise this might be easier.

Basically I guess I need grab to document.scrolltop and send it to the
server (so that I can return it when redering the page onload). But
<body onunload=> does not seem to work with my asp controls.

THANKS IN ADVANCE FOR ANY HELP OR INFORMATION!!!
 
Y

Yann-Erwan Perio

(e-mail address removed) 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top