如何在没有页面返回的情况下使用
Javascript刷新页面
顶端.
顶端.
我的页面使用计时器刷新,但问题是它每次重新加载时都会返回到顶层.它应该能够在重新加载时保留页面的当前位置.
附:
如有必要,欢迎额外的鼠标活动成为您建议的一部分.
我实际上是想在刷新时定位#idname,但我的HTML元素没有ID,只有类.
解决方法
如果您使用
JavaScript,此代码将起到作用.
var cookieName = "page_scroll"; var expdays = 365; // An adaptation of Dorcht's cookie functions. function setCookie(name,value,expires,path,domain,secure) { if (!expires) expires = new Date(); document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString()) + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : "; domain=" + domain) + ((secure == null) ? "" : "; secure"); } function getCookie(name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i,j) == arg) { return getCookieVal(j); } i = document.cookie.indexOf(" ",i) + 1; if (i == 0) break; } return null; } function getCookieVal(offset) { var endstr = document.cookie.indexOf(";",offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset,endstr)); } function deleteCookie(name,domain) { document.cookie = name + "=" + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : "; domain=" + domain) + "; expires=Thu,01-Jan-00 00:00:01 GMT"; } function saveScroll() { var expdate = new Date(); expdate.setTime(expdate.getTime() + (expdays*24*60*60*1000)); // expiry date var x = document.pageXOffset || document.body.scrollLeft; var y = document.pageYOffset || document.body.scrollTop; var data = x + "_" + y; setCookie(cookieName,data,expdate); } function loadScroll() { var inf = getCookie(cookieName); if (!inf) { return; } var ar = inf.split("_"); if (ar.length == 2) { window.scrollTo(parseInt(ar[0]),parseInt(ar[1])); } }
这通过使用cookie来记住滚动位置.
现在只需添加
onload="loadScroll()" onunload="saveScroll()"
你的身体标签,一切都会很好.
来源:http://www.huntingground.freeserve.co.uk/main/mainfram.htm?../scripts/cookies/scrollpos.htm