Is there a way to remember a users last position in the document?

Discussion in 'Javascript' started by Konrad Viltersten, May 13, 2005.

  1. Suppose you got a really long page and you'd like to enable
    the user (supposedly, there's only one but if it's not to
    difficult we could extend that to any number) not to have
    to scroll to the last position in the document he/she was
    viewing but simply auto-jump him/her to it. Is that doable
    at all using JS?

    I guess it would be somewhere in the vicinity of:
    - remember every scroll
    - save the line number to the users HDD
    - at next visit jump to the saved line

    I have seen pages that "remember" me by cookies so i
    guess that's a good start for the solution but the rest of
    the issue i'd love to get some help with.

    I've worked with Java and C++ for a few years so the
    prorgamming issues are not a problem. However, i'm
    still rather new to JS.

    --

    Vänligen
    Konrad
    ---------------------------------------------------

    Sleep - thing used by ineffective people
    as a substitute for coffee

    Ambition - a poor excuse for not having
    enough sense to be lazy

    ---------------------------------------------------
     
    Konrad Viltersten, May 13, 2005
    #1
    1. Advertising

  2. Konrad Viltersten

    askMe Guest

    askMe, May 13, 2005
    #2
    1. Advertising

  3. Konrad Viltersten

    Fred Oz Guest

    Konrad Viltersten wrote:
    > Suppose you got a really long page and you'd like to enable
    > the user (supposedly, there's only one but if it's not to
    > difficult we could extend that to any number) not to have
    > to scroll to the last position in the document he/she was
    > viewing but simply auto-jump him/her to it. Is that doable
    > at all using JS?
    >
    > I guess it would be somewhere in the vicinity of:
    > - remember every scroll
    > - save the line number to the users HDD
    > - at next visit jump to the saved line
    >
    > I have seen pages that "remember" me by cookies so i
    > guess that's a good start for the solution but the rest of
    > the issue i'd love to get some help with.
    >
    > I've worked with Java and C++ for a few years so the
    > prorgamming issues are not a problem. However, i'm
    > still rather new to JS.
    >


    You could store the value of scrollTop and pageYOffset and a page
    identifier (say filename) in a cookie onunload and, when the user
    requests on their next visit, scroll the page to that location.

    Have a poke around quirksmode in the viewport - browser compatibility
    page.

    <URL:http://www.quirksmode.org/> (frames)

    <URL:http://www.quirksmode.org/viewport/compatibility.html> (direct)


    --
    Fred
     
    Fred Oz, May 13, 2005
    #3
  4. Konrad Viltersten

    RobB Guest

    Konrad Viltersten wrote:
    > askMe wrote:
    > > You could use anchors.

    >
    >
    > Do you mean
    > a) dynamically set anchors that are changed at every scroll
    > or
    > b) anchors as in <a href="bip.html">bip</a>
    > ?


    (snip)

    He has no idea what he means. Notice his accompanying code sample.

    Fred's solution was correct. Here's a somewhat dated example:

    http://webreference.com/js/tips/991203.html

    Use ppk's properties (with object detection) for almost total browser
    coverage and proper degradation.
     
    RobB, May 13, 2005
    #4
  5. askMe wrote:
    > You could use anchors.



    Do you mean
    a) dynamically set anchors that are changed at every scroll
    or
    b) anchors as in <a href="bip.html">bip</a>
    ?

    If a - i'd like to know more. I don't seem to find any good
    info on that topic. If b - no really a solution for my part
    depending on various reasons.

    Thanks for trying, anyway.


    --

    Vänligen
    Konrad
    ---------------------------------------------------

    Sleep - thing used by ineffective people
    as a substitute for coffee

    Ambition - a poor excuse for not having
    enough sense to be lazy

    ---------------------------------------------------
     
    Konrad Viltersten, May 13, 2005
    #5
  6. > Have a poke around quirksmode in the viewport - browser
    > compatibility page.
    >
    > <URL:http://www.quirksmode.org/> (frames)
    >
    > <URL:http://www.quirksmode.org/viewport/compatibility.html>
    > (direct)



    Hmmm... The way i see it there's virtually no standard being
    followed by the different browsers (that, or there is one
    that most browsers have choosen not to follow very strictly
    for one reason or another). Sad...

    Thanks, by the way.

    --

    Vänligen
    Konrad
    ---------------------------------------------------

    Sleep - thing used by ineffective people
    as a substitute for coffee

    Ambition - a poor excuse for not having
    enough sense to be lazy

    ---------------------------------------------------
     
    Konrad Viltersten, May 13, 2005
    #6
  7. Konrad Viltersten

    RobB Guest

    RobB wrote:

    (snip)

    > Fred's solution was correct. Here's a somewhat dated example:
    >
    > http://webreference.com/js/tips/991203.html
    >
    > Use ppk's properties (with object detection) for almost total browser
    > coverage and proper degradation.


    OK, try this [untested].

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <script type="text/javascript">

    function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
    document.cookie = curCookie;
    }

    function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
    } else {
    begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
    }

    window.onload = function()
    {
    var y_scroll = getCookie('y_scroll');
    if (y_scroll)
    scrollTo(0, parseInt(y_scroll, 10));
    }

    window.onunload = function()
    {
    var y_scroll =
    window.pageYOffset ?
    pageYOffset :
    document.documentElement
    && 'undefined' != typeof document.documentElement.scrollTop ?
    document.documentElement.scrollTop :
    document.body ?
    document.body.scrollTop :
    null;
    var now = new Date();
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie('y_scroll', y_scroll || '', now);
    }

    </script>
    </head>
    <body>
    <pre>
    <script type="text/javascript">
    var z = 0;
    while (z++ < 100)
    document.writeln(z);
    </script>
    </pre>
    </body>
    </html>
     
    RobB, May 13, 2005
    #7
  8. RobB wrote:
    > Konrad Viltersten wrote:
    >> askMe wrote:
    >>> You could use anchors.

    >> Do you mean

    <snip>
    > (snip)
    >
    > He has no idea what he means. Notice his accompanying
    > code sample.


    There is no need to go as far as looking at code, the positing style
    alone is sufficient to indicate a worthless response.

    > Fred's solution was correct.


    The problem with Fred's suggestion is that the degree to which a page
    has previously been scrolled by a user will depend in part of the layout
    and flow of the document. So re-visiting the site with a browser window
    of different dimensions will tend to invalidate the scroll offsets from
    previous visits. And if the user has changed their default (or current)
    font size between visits then previous scroll offsets will also no
    longer be valid.

    It may be that the real solution to this problem is the provision of
    internal navigation on a page, so that the use can quickly get back to
    where they remember being.

    <snip>
    > Use ppk's properties (with object detection) for almost
    > total browser coverage and proper degradation.


    LOL

    Richard.
     
    Richard Cornford, May 13, 2005
    #8
  9. Konrad Viltersten

    RobB Guest

    Richard Cornford wrote:
    > RobB wrote:
    > > Konrad Viltersten wrote:
    > >> askMe wrote:
    > >>> You could use anchors.
    > >> Do you mean

    > <snip>
    > > (snip)
    > >
    > > He has no idea what he means. Notice his accompanying
    > > code sample.

    >
    > There is no need to go as far as looking at code, the positing style
    > alone is sufficient to indicate a worthless response.
    >
    > > Fred's solution was correct.

    >
    > The problem with Fred's suggestion is that the degree to which a page
    > has previously been scrolled by a user will depend in part of the

    layout
    > and flow of the document. So re-visiting the site with a browser

    window
    > of different dimensions will tend to invalidate the scroll offsets

    from
    > previous visits. And if the user has changed their default (or

    current)
    > font size between visits then previous scroll offsets will also no
    > longer be valid.
    >
    > It may be that the real solution to this problem is the provision of
    > internal navigation on a page, so that the use can quickly get back

    to
    > where they remember being.
    >
    > <snip>
    > > Use ppk's properties (with object detection) for almost
    > > total browser coverage and proper degradation.

    >


    The OP originally noted:

    <quote>
    Suppose you got a really long page and you'd like to enable
    the user (supposedly, there's only one but if it's not to
    difficult we could extend that to any number)...
    </quote>

    I took that to mean a certain degree of assurance of who those users
    might be - and under what conditions this 'solution' might be applied.
    In a general sense, you're quite right, HTML is not dtp and any fix
    which relies on window configuration is not reliable.

    > LOL
    >
    > Richard.


    I amuse you? I make you laugh? I'm here to ****in' amuse you?
    How am I funny, like a clown? What is so funny about me? What the ****
    is so funny about me? Tell me. Tell me what's funny. What percentage of
    browsers won't this work with (and degrade acceptably)? (rotfl)
     
    RobB, May 13, 2005
    #9
  10. > OK, try this [untested].
    >
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    > "http://www.w3.org/TR/html4/strict.dtd">

    <snip>

    Well, it didn't work so now i hate you!

    Just kidding. It really doesn't work but i hardly hate people
    at all, especially when they try to help my sorry donkey.
    I put a simple altert('something') in the two functions handling
    loading and unloading and what i discovered was that
    a) window.onload = function() {alert('bip');}
    gives me an altert (after all the images has been loaded)
    but
    b) window.onunload = function() {alert('bap');}
    produces nothing as i close the window.

    So, basically, i have two follow-up questions.
    1. How do i make the computer scream as i close the window?
    2. How do i make the computer screem BEFORE all the images
    on the site are loaded?

    The thing is that i follow a cartoon that is issued once a day
    and since it's tiresome to switch the days i simply set up a
    HTML-doc that handles all the days at once. The thing is that
    i'm too lazy to scroll (or use anchor) so i'd like the browser
    to remember where i was and jump to that position for me.

    Thanks in advance.

    By the way, i'm going on a trip to Poland and Czech tomorrow
    so if i don't reply until next saturday it's not because i'm not
    gratefull. It's because i'm enjoying my girlfriend on vacation.

    --

    Vänligen
    Konrad
    ---------------------------------------------------

    Sleep - thing used by ineffective people
    as a substitute for coffee

    Ambition - a poor excuse for not having
    enough sense to be lazy

    ---------------------------------------------------
     
    Konrad Viltersten, May 13, 2005
    #10
  11. Konrad Viltersten

    RobB Guest

    Konrad Viltersten wrote:
    > > <quote>
    > > Suppose you got a really long page and you'd like to enable
    > > the user (supposedly, there's only one but if it's not to
    > > difficult we could extend that to any number)...
    > > </quote>
    > >
    > > I took that to mean a certain degree of assurance of who those

    users
    > > might be - and under what conditions this 'solution' might be
    > > applied. In a general sense, you're quite right, HTML is not dtp
    > > and any fix which relies on window configuration is not reliable.

    >
    > Correct assumption! The "user" will most likely be me and
    > maybe (only maybe) a handfull of friends, none of which
    > is known to or expected to make any changes to the font
    > size, window position/size etc.
    >
    > So, for all you know (and please go with that) you got my
    > drift exactly! Nevertheless, it's nice to know that there
    > are certain limitations to what can be achieved.
    >
    > --
    >
    > Vänligen
    > Konrad
    > ---------------------------------------------------
    >
    > Sleep - thing used by ineffective people
    > as a substitute for coffee
    >
    > Ambition - a poor excuse for not having
    > enough sense to be lazy
    >
    > ---------------------------------------------------


    Hey Konrad...the above works for me locally. If you can't get
    window.onunload to run something is amiss. Might you be assigning it
    somewhere else ? (it's the same as same as <body onunload=".."> btw)

    Think about this while enjoying your girlfriend.
     
    RobB, May 13, 2005
    #11
  12. > <quote>
    > Suppose you got a really long page and you'd like to enable
    > the user (supposedly, there's only one but if it's not to
    > difficult we could extend that to any number)...
    > </quote>
    >
    > I took that to mean a certain degree of assurance of who those users
    > might be - and under what conditions this 'solution' might be
    > applied. In a general sense, you're quite right, HTML is not dtp
    > and any fix which relies on window configuration is not reliable.


    Correct assumption! The "user" will most likely be me and
    maybe (only maybe) a handfull of friends, none of which
    is known to or expected to make any changes to the font
    size, window position/size etc.

    So, for all you know (and please go with that) you got my
    drift exactly! Nevertheless, it's nice to know that there
    are certain limitations to what can be achieved.

    --

    Vänligen
    Konrad
    ---------------------------------------------------

    Sleep - thing used by ineffective people
    as a substitute for coffee

    Ambition - a poor excuse for not having
    enough sense to be lazy

    ---------------------------------------------------
     
    Konrad Viltersten, May 13, 2005
    #12
  13. > Hey Konrad...the above works for me locally. If you can't get
    > window.onunload to run something is amiss. Might you be
    > assigning it somewhere else ? (it's the same as same as <body
    > onunload=".."> btw)



    Well, it's really strange, since i get an event when loading...
    Anyway, i have set up the minimal code that reproduces the
    behavior. Does it work locally on you computer?

    <html><head><script type="text/javascript">
    window.onload = function() {
    alert ('The page has loaded successfully!');}
    window.onunload = function() {
    alert ('The page has started unloading!');}
    </script></head>

    <body><pre><script type="text/javascript">
    var z = 0;
    while (z++ < 200)
    document.writeln(z);
    </script></pre>
    </body></html>

    On my system only loading produces an alert...

    Is that too browser depending? I have IE6.0
    with SP2, as far as i can see.

    --

    Vänligen
    Konrad
    ---------------------------------------------------

    Sleep - thing used by ineffective people
    as a substitute for coffee

    Ambition - a poor excuse for not having
    enough sense to be lazy

    ---------------------------------------------------
     
    Konrad Viltersten, May 13, 2005
    #13
  14. Konrad Viltersten

    RobB Guest

    Konrad Viltersten wrote:
    > > Hey Konrad...the above works for me locally. If you can't get
    > > window.onunload to run something is amiss. Might you be
    > > assigning it somewhere else ? (it's the same as same as <body
    > > onunload=".."> btw)

    >
    >
    > Well, it's really strange, since i get an event when loading...
    > Anyway, i have set up the minimal code that reproduces the
    > behavior. Does it work locally on you computer?
    >
    > <html><head><script type="text/javascript">
    > window.onload = function() {
    > alert ('The page has loaded successfully!');}
    > window.onunload = function() {
    > alert ('The page has started unloading!');}
    > </script></head>
    >
    > <body><pre><script type="text/javascript">
    > var z = 0;
    > while (z++ < 200)
    > document.writeln(z);
    > </script></pre>
    > </body></html>
    >
    > On my system only loading produces an alert...
    >
    > Is that too browser depending? I have IE6.0
    > with SP2, as far as i can see.
    >
    > --
    >
    > Vänligen
    > Konrad
    > ---------------------------------------------------
    >
    > Sleep - thing used by ineffective people
    > as a substitute for coffee
    >
    > Ambition - a poor excuse for not having
    > enough sense to be lazy
    >
    > ---------------------------------------------------


    This has been discussed here previously:

    <URL:
    http://groups-beta.google.com/group...adf256?q=sp2 onunload&rnum=1#2d8a26e4f3adf256
    >


    Try window.onbeforeunload...
     
    RobB, May 13, 2005
    #14
  15. >> Well, it's really strange, since i get an event when loading...
    >> Anyway, i have set up the minimal code that reproduces the
    >> behavior. Does it work locally on you computer?
    >>
    >> window.onunload = function() {
    >> alert ('The page has started unloading!');}

    >
    > This has been discussed here previously:
    >

    http://groups-beta.google.com/group...adf256?q=sp2 onunload&rnum=1#2d8a26e4f3adf256
    > Try window.onbeforeunload...



    Aha, there we go! Got it. It still doesn't work as the
    computer doesn't scroll to the last position but from
    here i think i'd like to fight it on my own for a while.
    I'll make sure to get back bitching and whining if i
    fail to do so, hehe.

    Thanks!

    --

    Vänligen
    Konrad
    ---------------------------------------------------

    Sleep - thing used by ineffective people
    as a substitute for coffee

    Ambition - a poor excuse for not having
    enough sense to be lazy

    ---------------------------------------------------
     
    Konrad Viltersten, May 14, 2005
    #15
  16. JRS: In article <>,
    dated Fri, 13 May 2005 09:31:46, seen in news:comp.lang.javascript, RobB
    <> posted :
    >
    > var now = new Date();
    > now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);


    var now = new Date();
    now.setMonth(now.getMonth() + 12);

    is simpler, while avoiding the question of whether getFullYear and
    setFullYear are supported.

    var now = new Date(+new Date()+32e9);

    gives a little over a year, and should be fast if the browser is smart
    about object lifetimes.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, May 14, 2005
    #16
  17. Konrad Viltersten

    askMe Guest

    Richard Cornford wrote:
    > RobB wrote:
    > > Konrad Viltersten wrote:
    > >> askMe wrote:
    > >>> You could use anchors.
    > >> Do you mean

    > <snip>
    > > (snip)
    > >
    > > (snip)
    > > Fred's solution was correct.

    >
    > The problem with Fred's suggestion is that the degree to which a page
    > has previously been scrolled by a user will depend in part of the

    layout
    > and flow of the document.


    Let alone if the user returns to the page from a different computer.
    If his cookies just returned the user to an anchor, he won't need to
    bother with screen dimensions, coordinates, scroll positions, type of
    browser... and the list goes on.

    > So re-visiting the site with a browser window
    > of different dimensions will tend to invalidate the scroll offsets

    from
    > previous visits.


    Exactly!

    > And if the user has changed their default (or current)
    > font size between visits then previous scroll offsets will also no
    > longer be valid.


    Another good point.

    > It may be that the real solution to this problem is the provision of
    > internal navigation on a page, so that the use can quickly get back

    to
    > where they remember being.


    Yes. Anchors and a click here to return link/button gets my vote,
    especially since its for a small audience.

    > (snip)
    > Richard.


    There are also lots of form scripts that return the user to the last
    position, but they also rely on the user clicking a button to mark the
    spot. He only needs to use hidden fields, getElementByID and focus to
    go that route. Otherwise, he can just have the preset anchors that get
    'saved' when the use is tired of reading and wants to mark a section or
    paragraph to return to on next visit.

    Browsers know what the coordinates are and use that data to return a
    user to the same screen position when the back button is pressed. But,
    as you pointed out, that deals only with the current window within the
    current browser. Anchors don't care about the window or the browser
    and neither do forms.

    I would love to see a javascript that relates scroll position to
    page/character position without user intervention. Never seen one.
    Doubtful that I will.

    http://www.askblax.com
     
    askMe, May 15, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel
    Replies:
    1
    Views:
    8,569
    Steven Cheng[MSFT]
    Jul 16, 2004
  2. Frank Miverk
    Replies:
    0
    Views:
    708
    Frank Miverk
    Apr 18, 2007
  3. Bobby Edward

    How remember page position on postback

    Bobby Edward, Oct 1, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    653
    Bobby Edward
    Oct 1, 2008
  4. Replies:
    3
    Views:
    192
  5. Tom

    Remember web page position help.

    Tom, Apr 18, 2006, in forum: Javascript
    Replies:
    1
    Views:
    111
Loading...

Share This Page