js控制页面刷新时,滚动条位置不变。兼顾IE5,IE6【转】
原文地址http://hi.baidu.com/huangbuji/blog/item/528247c27965f2130ef477fe.html
页面加载时从cookie获取滚动条位置信息,然后附值给滚动条
function window.onload()
{
var arr;
if(arr=document.cookie.match(/scrollTop=([^;]+)(;|$)/))
document.documentElement.scrollTop=parseInt(arr[1]);
document.body.scrollTop=parseInt(arr[1]);
}
页面刷新前保存滚动条位置信息到cookie
function window.onbeforeunload(){
var scrollPos;
if (typeof window.pageYOffset != 'undefined') {
scrollPos = window.pageYOffset;
}
else if (typeof document.compatMode != 'undefined' &&
document.compatMode != 'BackCompat') {
scrollPos = document.documentElement.scrollTop;
}
else if (typeof document.body != 'undefined') {
scrollPos = document.body.scrollTop;
}
document.cookie="scrollTop="+scrollPos;
}