Browser does not remember position of page last viewed

后端 未结 4 698
南笙
南笙 2021-01-01 00:28

I have done a few searches for this issue and I have come up empty handed. I hope somebody can clarify things for me and point me in the right direction.

Pro

相关标签:
4条回答
  • 2021-01-01 00:49

    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.

    0 讨论(0)
  • 2021-01-01 00:52

    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:

    • Stack Overflow: Is it possible to persist (without reloading) AJAX page state across BACK button clicks?
    • YUI Browser History Manager
    • Ajax Patterns: Unique URLs
    0 讨论(0)
  • 2021-01-01 00:52

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 01:02

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

    0 讨论(0)
提交回复
热议问题