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 14 of 14
  1. #1
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    retain vertical scroll position after leaving page

    I am using this header( 'Location: indexfull2.php' ) ; to return to the database page after clicking a link which sends data, it works just fine, you never really leave the database page, but if you were miles down the page (3000 rows) you're now back at the top. I know I could have pages for the database but I would rather keep it as it is if I can find a solution.
    There are scripts available for similar problems but they don't work with this, anyone have an idea

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Use unique id's in one of you tags for each row and send the browser to that id:

    Code:
    header( 'Location: indexfull2.php#uniqueid' ) ;

  • #3
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure about how to get the unique id to go with the page on leaving tried this @$_GET['uniqid(rand (),true)'];

    and

    $a = @$_GET['#uniqid(rand (),true)'];

    and then
    header( 'Location: indexfull2.php.#$a' ) ;
    but no joy

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Wrong method. If you're using anchors, for example:

    Code:
    <a id="thistagsid" href="script.php?tagid=thistagsid">Link to whatever script</a>
    In the target script:

    Code:
    $id = ((isset($_GET['tagid'])) ? '#'.intval($_GET['tagid']) : '');
    
    header('Location: indexfull2.php'.$id);

  • #5
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Matt but I think I may have a problem with this because there is an href already there, can't have two href ??

    <a href="select.php?id=<?php echo $row ['id'].'">'.sprintf("%04d",$row ['id']); ?></a>

    Maybe adjust this one to use the same id

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    It was an *example*. Same as was suggested by Old Pedant in the other thread, examples are there to guide, not to be used verbatim. Add the id to your existing code. Btw, that array syntax isn't valid. Change your *existing* link to this:

    Code:
    <a id="<?php echo $row['id'].'" href="select.php?id='.$row['id'].'">'.sprintf("%04d",$row['id']); ?></a>
    Last edited by MattF; 11-11-2010 at 05:55 PM.

  • #7
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No Matt I wasn't sure that the existing id could be used, so I have tried that but may have to change something because it doesn't work. I'll just show you what I've done

    This is the link to the page I will bounce back from as per your changes

    <a id="<?php echo $row['id'].'" href="select.php?id='.$row['id'].'">'.sprintf("%04d",$row['id']); ?></a>

    And I have these two in that page with some changes, the page does return but not to the same scroll position, should the address bar show the id after the indexfull2.php

    $back = isset($_GET['id']) ? '#'.intval($_GET['id']) : '';

    header( 'Location: indexfull2.php'.$back );

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    That looks right enough. If you echo $back out on the second page, before the header call, is it set?

  • #9
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes the echo shows #4 or #12 or whatever, just realised that it is picking up the original id number not the padded 0004 or 0012

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by okeddy View Post
    yes the echo shows #4 or #12 or whatever, just realised that it is picking up the original id number not the padded 0004 or 0012
    That's what the id should be like, without the padding. I might just be having a dumb moment, but I can't see why that shouldn't be working.

  • #11
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So it shouldn't pickup the padded id, I suppose that is hidden apart from when it prints.
    Should the address bar show the id at the end it when it returns?

  • #12
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The code was right but I didn't have it at the top of the page along with the header, I had the header in a separate <??>

    PHP Code:
    $back = isset($_GET['id']) ? '#' .intval($_GET ['id']) : '';
    header'Location: indexfull2.php'.$back ); 
    Thanks Matt once again you're great help..

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Javascript example ... I found on the internet. Circa 2005

    Test this out:

    PHP Code:
    <html>
    <
    head>
    <
    title>Scroll Position Saver Example</title>
    <
    script language="JavaScript" src="scroll_saver.js"></script>
    </head>
    <body onload="restoreScroll()" onUnload="saveScroll()">

    View Javascript file: <a href="scroll_saver.js">scroll_saver.js</a><br /><br />

     1.. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     10. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     20. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     30. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     40. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     50. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     60. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     70. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     80. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     90. <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     100 <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     110 <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     120 <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     130 <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />
     ... <br />

    </body>
    </html> 

    The Javascript ... called: "scroll_saver.js" ...
    PHP Code:
    /*****************************************************************************
     Window scroll saver.
     Munsifali Rashid.  mUnit Limited (www.munit.co.uk).  April 2005.
     
     Simply include this file into any page and it will retain the page scroll
     position across postbacks.
     
     If you are using the window.onload or window.unload events for anything,
     you will have to tweak the code a little.  saveScroll() must be called when
     the page unloads, and restoreScroll() must be called when the page loads.
    *****************************************************************************/

    var COOKIE_NAME "MyCookieName";

    function 
    cookiemanager(allcookies)
    {
        
    this.name        COOKIE_NAME;
        
    this.items        = new Array();
        
    this.add        =    function(keyval)
                            {
                                
    this.items[key] = val;
                            }
        
    this.makeCookie    =    function()
                            {
                                var 
    "";
                                for (
    key in this.items+= key ":" escape(this.items[key]) + "&";
                                
    a.substring(0a.length-1);
                                
    this.name "=" a;
                                return 
    a;
                            }
        
    this.saveCookie    =    function()
                            {
                                
    document.cookie this.makeCookie();
                            }
        if (
    allcookies.indexOf(this.name) == -1) return;
        var 
    start allcookies.indexOf(this.name) + this.name.length 1;
        var 
    end allcookies.indexOf(";"start);
        if (
    end == -1end allcookies.length;
        var 
    cookie allcookies.substring(startend);
        var 
    cookie.split("&");
        for (var 
    i=0a.lengthi++)
        {
            var 
    a[i].split(":");
            
    this.items[b[0]] = unescape(b[1]);
        }
    }
    var 
    cm = new cookiemanager(document.cookie);

    function 
    saveScroll()
    {
        var 
    scrolltop document.body.scrollTop;
        
    cm.add("scrolltop"scrolltop);
        
    cm.saveCookie();
    }
    function 
    restoreScroll()
    {
        if (
    document.getElementById) if (cm.items["scrolltop"]) document.body.scrollTop cm.items["scrolltop"];
    }
    if ((
    document.getElementById) && (!window.onunload))    window.onunload    saveScroll;
    if ((
    document.getElementById) && (!window.onload))        window.onload    restoreScroll

  • #14
    New Coder
    Join Date
    Nov 2010
    Location
    Scotland
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thx for that


  •  

    Posting Permissions

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