faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

29 of 36 people (81%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How kan I refresh a window with scrollbars and come back to the same position I had before.

Jun 24th, 2002 09:53
David Blackledge, Kári Indridason,


What you would need to do is save the current scroll position, then 
refresh, passing the current scroll position into the page.  When the 
page loads, it scrolls to the passed-in position:
function refreshSameLoc() {
 // get X
 var X = 
(window.pageXOffset?window.pageXOffset:window.document.body.scrollLeft);
 var Y = 
(window.pageYOffset?window.pageYOffset:window.document.body.scrollTop);
 // it depends how you're refreshing, but let's assume you're reloading.
 window.location.href = window.location.href+"?x="+X+"&y="+Y;
}
function scrollOnLoad() {
 var s = window.location.search;
 var idx1 = s.indexOf("?x=");
 if(idx1 != -1) {
   var idx2 = s.indexOf("&y=",idx1);
   var X = parseInt(s.substring(idx1+3,idx2));
   var Y = parseInt(s.substring(idx2+3));
   window.scrollTo(X,Y);
 }
}
... BODY onload="scrollOnLoad()" ...
... onclick="refreshSameLoc()" ...
David.
http://David.Blackledge.com