基本上下面的代码可以正常工作:它会根据'rate'的值刷新页面,并在页面刷新后保持页面滚动位置。
我要做的是添加一个选择表来调整刷新率,换句话说就是'rate'的值。凭借我在js中的经验,我能够添加选择表并按所选值设置速率,问题是当页面刷新时,select表中的值默认为表中的第一个值,在我的情况下为'5' 。第二个问题是,当我更改表格中的值时,每次选择新值时页面都会滚动到顶部。
var rate = 10; // refresh rate
function refresh() {
document.cookie = 'scrollTop=' + filterScrollTop();
document.cookie = 'scrollLeft=' + filterScrollLeft();
document.location.reload(true);
}
function getCookie(name) {
var start = document.cookie.indexOf(name + "=");
var len = start + name.length + 1;
if (((!start) && (name != document.cookie.substring(0, name.length))) || start == -1) return null;
var end = document.cookie.indexOf(";", len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len, end));
}
function deleteCookie(name) {
document.cookie = name + "=" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function setupRefresh() {
var scrollTop = getCookie("scrollTop");
var scrollLeft = getCookie("scrollLeft");
if (!isNaN(scrollTop)) {
document.body.scrollTop = scrollTop;
document.documentElement.scrollTop = scrollTop;
}
if (!isNaN(scrollLeft)) {
document.body.scrollLeft = scrollLeft;
document.documentElement.scrollLeft = scrollLeft;
}
deleteCookie("scrollTop");
deleteCookie("scrollLeft");
setTimeout("refresh()", rate * 1000);
}
function filterResults(win, docEl, body) {
var result = win ? win : 0;
if (docEl && (!result || (result > docEl))) result = docEl;
return body && (!result || (result > body)) ? body : result;
}
// Setting the cookie for vertical position
function filterScrollTop() {
var win = window.pageYOffset ? window.pageYOffset : 0;
var docEl = document.documentElement ? document.documentElement.scrollTop : 0;
var body = document.body ? document.body.scrollTop : 0;
return filterResults(win, docEl, body);
}
// Setting the cookie for horizontal position
function filterScrollLeft() {
var win = window.pageXOffset ? window.pageXOffset : 0;
var docEl = document.documentElement ? document.documentElement.scrollLeft : 0;
var body = document.body ? document.body.scrollLeft : 0;
return filterResults(win, docEl, body);
}
我没有把我的尝试包括在内,但如果需要的话,我可以把它包括在内,基本上我只是想找到正确的方法,
感谢