Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Refreshing Page Keeping position

    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

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you want to store said values in a cookie, so onload the page get's redrawn based on the cookie.

  • #3
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are looking for: document.body.scrollTop

    Try this:

    Code:
    <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

  • #5
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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()">

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Solution without using cookie.
    window.name is a persistent property.
    Code:
    <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>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    P_M
    P_M is offline
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    #!/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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •