...

View Full Version : Retain vertical scroll when going back to search results



htcilt
02-17-2010, 03:23 PM
I have a form that when submitted displays the results in rows (search.php)
Each row has a link to more information (detail.php).

I have the parameters in a session so that I can get back to search.php from detail.php with parameter, pagination etc intact.

However, I need to find a way to put the scroll back to here it was when the user clicked on the 1st link (in detail.php).
The reason for this is they may loose where they were when scrolling through the results.

I know the scroll position can be accessed/set with jquery, but I'm not sure passing client information to php is possible. The scroll position needs to be retrieved when the user clicks on the link to detail.php and ideally put into the session, but I'm not sure how?

DaiWelsh
02-17-2010, 05:05 PM
If I understand correctly, what I would suggest is having a named anchor on each result and linking back using local linking e.g.



Blah blah <a href="detail.php?id=1" name="link1">Details</a>
Blah blah <a href="detail.php?id=2" name="link2">Details</a>
Blah blah <a href="detail.php?id=3" name="link3">Details</a>


then if link 3 was clicked (you know this in details.php anyway) you make the link back go to search.php#link3. The browser should then jump the page down to the right point automatically without needing to figure out exact scroll positons. In fact you could probably just put a named anchor on the one result that was clicked if you want to save a few bytes.

HTH,

Dai

htcilt
02-18-2010, 10:51 AM
Hi,

Thanks for the reply.
This is almost working. Its fine in IE but not firefox. The page jumps to the bottom instead of for example half way.

The strange thing is when I hit return in the address bar, the page jumps to where it is supposed to be, so the anchor is correct.

Here is the php generating the link


echo "<a name=\"hash".$row['ID']."\" id=\"hash".$row['CL_NO']."\"></a><a href=\"detail.php?ID=".$row['ID']."\"><strong>".$row['TITLE']."</strong></a>\n";

and the output html


<a name="hashCS19" id="hashCS19"></a><a href="detail.php?ID=CS19"><strong>Title goes here</strong></a>

Can anyone see why only firefox would jump to the bottom?

htcilt
02-18-2010, 11:56 AM
I've done a bit more testing.
I currently have 20 rows returned per page. To get the scrolls, I reduced the size of the window. This is where firefox starts to fail. If I maximise the window and increase the rows returned to 80 (to gain scrollbar) it works!

So it seems that in FF if the maximsed window doesn't need to scroll, the anchors wont work when the windows is reduced even through the scrollbar is introduced.

IE works perfectly!

DaiWelsh
02-18-2010, 02:20 PM
Sounds like a firefox, um, idiosynchrasy ;)

blue642
02-18-2010, 04:47 PM
I just tested this with no PHP involved....

(just a raw unordered list of links.)
I noticed in your example you went back to the empty link in front of the link you want... I don't understand why, did you run into an issue?


<ul>
<li><a href="#linked">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a name="linked" href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>


either way I shrunk the window to allow scrolling and clicked the first link (in FF) this jumped me to the proper link...

I tried again with your setup..


<ul>
<li><a href="#linked">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a name="linked" id="linked"></a><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>


this jumped me down to the bottom just like in your issue..
So if possible, try merging the name attribute over to the main link...

htcilt
02-18-2010, 08:41 PM
Thanks blue642,

I'll make that amend tomorrow and see if FF behalves differently.
I think I went back to the separate anchor before the item because I was using eBay as a reference.
But the thing with eBay is every page has a vertical scroll - even if only 1 row is returned. It might even be that the problem exists for eBay too, but isn't apparent due to the amount of scrolling involved on the results page.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum