...

View Full Version : Refreshing Page Keeping position



projectfocus
07-01-2004, 03:32 PM
I am looking for a adaptation to the reload function.

When this is called it will reload the page. I don't want the page to reload but I want it to refresh. Keeping the possition that you where on the page that you are viewing.

The way I would think that you would do it was to save the XY values of the webpage to variables then on reload to send them back. The only issue is that I can't find anyway of getting these values.

Can anyone help

jbot
07-01-2004, 04:32 PM
you want to store said values in a cookie, so onload the page get's redrawn based on the cookie.

projectfocus
07-01-2004, 05:34 PM
what values are they. Does anyone know the name of the values that I want to save. can do it as a cookie. The problem is that I don't know what the things are called.

Willy Duitt
07-01-2004, 06:26 PM
You are looking for: document.body.scrollTop

Try this:



<script type="text/javascript">
<!--//
function setScroll(){
var d = document;
d.cookie=escape(d.location.pathname)+'ScrollPos='+escape(d.body.scrollTop);
}


function chkScroll(){
var d = document;
var n = escape(d.location.pathname)+'ScrollPos=';
var s = d.cookie.indexOf(n);
if(s > -1){ var e = d.cookie.indexOf(';',s+n.length) };
if(e < 0){
e = d.cookie.length;
d.body.scrollTop=unescape(d.cookie.substring(s+n.length,e));
}
}
//-->
</script>
</head>

<body onunload="setScroll()" onload="chkScroll()">
<div style="height:3600px">&nbsp;</div>


.....Willy

projectfocus
07-02-2004, 11:08 AM
That is a good bit of code the only issue is that the refresh function is not included. Where would this be put as it will need to be invoked. The issue I can see is that this is done by a
setTimeout("window.location.reload()",30000)

Do I have to change the function it does on the reload

As follows

<script type="text/javascript">
<!--//
function reloadpage()
{
setTimeout("setsScrollreload()",30000)

}

function setScrollreload()
{
var d = document;
d.cookie=escape(d.location.pathname)+'ScrollPos='+escape(d.body.scrollTop);

window.location.reload()
}


function chkScroll(){
var d = document;
var n = escape(d.location.pathname)+'ScrollPos=';
var s = d.cookie.indexOf(n);
if(s > -1){ var e = d.cookie.indexOf(';',s+n.length) };
if(e < 0){
e = d.cookie.length;
d.body.scrollTop=unescape(d.cookie.substring(s+n.length,e));
}
}
//-->
</script>

<body onunload="reloadpage()" onload="chkScroll()">

Would that work

projectfocus
07-02-2004, 11:14 AM
Sorry that was wrong but I have tried this and this does not work. Please advise why.

<script type="text/javascript">
<!--//

function setScroll()
{
var d = document;
d.cookie=escape(d.location.pathname)+'ScrollPos='+escape(d.body.scrollTop);

}


function chkScroll(){
var d = document;
var n = escape(d.location.pathname)+'ScrollPos=';
var s = d.cookie.indexOf(n);
if(s > -1){ var e = d.cookie.indexOf(';',s+n.length) };
if(e < 0){
e = d.cookie.length;
d.body.scrollTop=unescape(d.cookie.substring(s+n.length,e));
}
setTimeout("window.location.reload()",30000)

}
//-->
</script>


<body onunload="setScroll()" onload="chkScroll()">

glenngv
07-02-2004, 12:43 PM
Solution without using cookie.
window.name is a persistent property.


<html>
<head>
<script type="text/javascript">
function doScroll(){
if (window.name) window.scrollTo(0, window.name);
}
</script>
</head>
<body onload="doScroll()" onunload="window.name=document.body.scrollTop">
<form>
<pre>
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
</pre>
<input type="button" value="Reload" onclick="location.reload()" />
</form>
</body>
</html>

P_M
02-05-2005, 03:29 PM
Hello,

I found this thread through an internet search to find the answer to this exact problem (keeping the page position after a javascript refresh), and I'm pleased to say that glenngv's piece of code works a treat. However, it doesn't work when the HTML is generate by a perl script.

Anybody know why that might be and if the problem can be solved. Below is a simple perl script which illustrates the problem.



#!/usr/bin/perl

print "Content-type: text/HTML", "\n\n";

print <<END_OF_BLOCK;

<html>
<head>
<script type="text/javascript">
function doScroll(){
if (window.name) window.scrollTo(0, window.name);
}
</script>
</head>
<body onload="doScroll()" onunload="window.name=document.body.scrollTop">
<script language="JavaScript">
<!--
// we put this here so we can see something change
document.write('<b>' + (new Date).toLocaleString() + '</b>');
//-->
</script>

<pre>
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
filler
</pre>
<form method="GET" action="test.pl">
<p><input type="button" onclick="location.reload()" value="Reload" name="button1"></p>
</form>
<a name="anchor">
</body>
</html>



END_OF_BLOCK



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum