Browser does not remember position of page last viewed

烈酒焚心 提交于 2019-11-30 09:57:42
Anirvan

If this is incredibly important, I'd suggest investigating the following:

  • add ids to each outgoing link
  • use JavaScript to capture the onClick for the links
  • when a link is clicked, redirect the user to that link's id fragment identifier, then link out as desired

When the user hits the back button, they'll return to that specific link, e.g. http://www.example.com/#link27 instead of http://www.example.com/

You may be able to get some ideas from here:

You can use javascript and jquery to set the scroll position of the window and cookies to store the position to scroll to. In the javascript of the page with the search results you could have something like this:

var COOKIE_NAME = "scrollPosition";
$(document).ready( function() {

    // Check to see if the user already has the cookie set to scroll
    var scrollPosition = getCookie(COOKIE_NAME);
    if (scrollPosition.length > 0) {
        // Scroll to the position of the last link clicked
        window.scrollTo(0, parseInt(scrollPosition, 10));
    }

    // Attach an overriding click event for each link that has a class named "resultLink" so the
    // saveScrollPosition function can be called before the user is redirected.
    $("a.resultLink").each( function() {
        $(this).click( function() { 
            saveScrollPosition($(this));
        });
    });

});

// Get the offset (height from top of page) of the link element
// and save it in a cookie.
function saveScrollPosition(link) {
    var linkTop = link.offset().top;
    setCookie(COOKIE_NAME, linkTop, 1);
}

// Boring cookie helper function
function getCookie(name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(name + "=");
        if (c_start != -1) {
            c_start = c_start + name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end ==- 1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
// Another boring cookie helper function
function setCookie(name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = name + "=" + escape(value) +
        ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

This assumes your search result links have class="resultLink".

The first part of the answer is that you use anchors to land on a page somewhere other than the top. So if I have this in my html at the bottom of my page:

<a name="f"></a>

then I can have the user land there by appending the anchor to the end of he url:

http://www.bullionvalues.com/glossary.aspx#f

So, if you are talking about ASP.Net you can place the anchor in a hidden field on the page info page and then read it from the search page by using: Page.PreviousPage property.

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.PreviousPage != null)
    {
        Object o = PreviousPage.FindControl("hfAnchor");
        if (o != null)
        {
            HiddenField hf = o as HiddenField;
            Response.Redirect(Request.Url+"#"+hf.Value);
        }
    }
}
VinkoCM

I fixed this issue by sending headers with php. This was my solution:

header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: store, cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", FALSE);

Thanks to everybody for the help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!