使用asp.net的时候,Postback时页面会刷新,当页面很长时,客户体验非常不好,下面这段脚本实现保持当前滚动条位置,当然如果您使用ajax,不会有这个问题:
window.onload
=
function
()
{
if (getcookie( " height " ) != null )
document.body.scrollTop = getcookie( " height " );
}
window.onunload = function ()
{
setcookie( " height " ,document.body.scrollTop);
}
function getcookie(sName)
{
var aCookie = document.cookie.split( " ; " );
for ( var i = 0 ; i < aCookie.length; i ++ )
{
var aCrumb = aCookie[i].split( " = " );
if (sName == aCrumb[ 0 ])
return unescape(aCrumb[ 1 ]);
}
return null ;
}
function setcookie(sName, sValue)
{
date = new Date();
date.setTime(date.getTime() + 5000 ); // 5 second
document.cookie = sName + " = " + escape(sValue) + " ; expires= " + date.toGMTString();
}
{
if (getcookie( " height " ) != null )
document.body.scrollTop = getcookie( " height " );
}
window.onunload = function ()
{
setcookie( " height " ,document.body.scrollTop);
}
function getcookie(sName)
{
var aCookie = document.cookie.split( " ; " );
for ( var i = 0 ; i < aCookie.length; i ++ )
{
var aCrumb = aCookie[i].split( " = " );
if (sName == aCrumb[ 0 ])
return unescape(aCrumb[ 1 ]);
}
return null ;
}
function setcookie(sName, sValue)
{
date = new Date();
date.setTime(date.getTime() + 5000 ); // 5 second
document.cookie = sName + " = " + escape(sValue) + " ; expires= " + date.toGMTString();
}