Search code examples
asp.netbrowserbookmarks

How to Override Browser Remembering Position when loading a new Version of a Page?


I have a page called results.aspx. It shows a list of search results. I have a transport (for want of a better term) at the top and bottom of each page. One of these:

Previous   1   2   [3]   4   5   Next 

Each of the text elements in the transport has a link in it, like this:

<a href="results.aspx?page=4" />

Page number varies, of course, based on the intended destination.

My problem is that because the root page name is the same, results.aspx, the browser always scrolls to the same position on the newly loaded page as it was on the page I just clicked away from. Or at least that's what I'm assuming is happening.

I've tried to create tags like

<a name="top">
<a id="top">

and change my link to either of

<a href="results.aspx?page=4#top" />
<a href="results.aspx#top?page=4" />

To no avail. I should point out that this is an inherited code base, so there could be nasties in the javascript that are overriding default behaviors.

Is there an obvious way to fix this? If it should be working by default, is there something in the code that I should be looking for that I should change or remove?

-- EDIT --

Here is a sample of the generated code taken from a browser:

<!-- Page Transport --> 
<ul> 
<li class="gray">first</li> 
<li class="gray">previous</li> 
<li class="current">1</li> 
<li><a href="lodging/results.aspx?page=2">2</a></li> 
<li><a href="lodging/results.aspx?page=3">3</a></li> 
<li><a href="lodging/results.aspx?page=4">4</a></li> 
<li><a href="lodging/results.aspx?page=2">next</a></li> 
<li><a href="lodging/results.aspx?page=20">last</a></li> 
</ul>

Yes, they're just hrefs and for the life of me, I cannot figure out why a browser would remember its position on the page!

-- SOLUTION --

Turns out the original developer included a javascript file, scrollsaver.min.js, which deliberately implemented this behavior. Removing this reference eliminated the behavior.


Solution

  • Are you sure that those links are <a> tags and not buttons or <a> tags with onclick. If they are plain <a> tags what you describe should not happen. If those are postback buttons, somewhere in the code the scroll position is maintained by using MaintainScrollPositionOnPostback. Search for it. Once you can confirm that it is used, you can use Resetting Scroll Position to overcome this.