"ojorus" <oj****@hotmail.com> wrote in message
news:Q4FFc.980$vH5.146@amstwist00...
Hi!
I just wonder how I can save a page's scroll position with javascript.
This is what I use (for IE6 anyway)...
//================================================== ===
// Scrolling
//================================================== ===
// Call this from the body onload on each postback in
// order to preserve the body's scroll position.
function restoreScrollPos()
{
// copy references to form's submit and onsubmit methods
document.forms[0].doSubmit1 = document.forms[0].onsubmit;
document.forms[0].doSubmit2 = document.forms[0].submit;
// change references to proprietary methods
document.forms[0].onsubmit = saveScrollPos1;
document.forms[0].submit = saveScrollPos2;
//recover the previous scroll poition from the cookie
var posX = parseInt(getCookie("scrollPosX"));
var posY = parseInt(getCookie("scrollPosY"));
//destroy the cookie
deleteCookie("scrollPosX");
deleteCookie("scrollPosY");
//scroll to the recovered position
window.scroll(posX, posY);
return true;
}
//save the scroll poition to the cookie (set expire to small value)
function saveScrollPos()
{
var expiry = new Date();
expiry.setSeconds(expiry.getSeconds() + 10);
setCookie("scrollPosX", window.document.body.scrollLeft, expiry);
setCookie("scrollPosY", window.document.body.scrollTop, expiry);
}
function saveScrollPos1()
{
saveScrollPos();
document.forms[0].doSubmit1();
}
function saveScrollPos2()
{
saveScrollPos();
document.forms[0].doSubmit2();
}
//================================================== ===
// Cookies
//================================================== ===
function setCookie(name, value, expires, path, domain, secure)
{
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
document.cookie = curCookie;
}
function getCookie(name)
{
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0)
return null;
}
else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie (name)
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
// This cookie is history
var cval = getCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}