...

View Full Version : retain vertical scroll position after leaving page



okeddy
11-11-2010, 12:31 PM
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 :(

MattF
11-11-2010, 12:35 PM
Use unique id's in one of you tags for each row and send the browser to that id:



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

okeddy
11-11-2010, 02:59 PM
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

MattF
11-11-2010, 03:43 PM
Wrong method. If you're using anchors, for example:



<a id="thistagsid" href="script.php?tagid=thistagsid">Link to whatever script</a>


In the target script:



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

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

okeddy
11-11-2010, 04:33 PM
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

MattF
11-11-2010, 05:43 PM
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:



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

okeddy
11-11-2010, 10:25 PM
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 );

MattF
11-11-2010, 10:39 PM
That looks right enough. If you echo $back out on the second page, before the header call, is it set?

okeddy
11-11-2010, 10:59 PM
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

MattF
11-11-2010, 11:04 PM
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.

okeddy
11-11-2010, 11:09 PM
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?

okeddy
11-12-2010, 10:57 AM
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 <??>


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

Thanks Matt once again you're great help..:thumbsup:

mlseim
11-12-2010, 12:35 PM
Javascript example ... I found on the internet. Circa 2005

Test this out:



<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" ...


/*****************************************************************************
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(key, val)
{
this.items[key] = val;
}
this.makeCookie = function()
{
var a = "";
for (key in this.items) a += key + ":" + escape(this.items[key]) + "&";
a = a.substring(0, a.length-1);
a = 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 == -1) end = allcookies.length;
var cookie = allcookies.substring(start, end);
var a = cookie.split("&");
for (var i=0; i < a.length; i++)
{
var b = 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;

okeddy
11-12-2010, 04:22 PM
Thx for that



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum