473,472 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

---------------------------------------------------

Jul 23 '05 #1
16 1578
You could use anchors.

http://www.askblax.com

Jul 23 '05 #2
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
Jul 23 '05 #3
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

---------------------------------------------------

Jul 23 '05 #4
> 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

---------------------------------------------------

Jul 23 '05 #5
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.

Jul 23 '05 #6
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>

Jul 23 '05 #7
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.
Jul 23 '05 #8
> 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

---------------------------------------------------

Jul 23 '05 #9
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)

Jul 23 '05 #10
> <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

---------------------------------------------------

Jul 23 '05 #11
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.

Jul 23 '05 #12
> 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

---------------------------------------------------

Jul 23 '05 #13
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/...8a26e4f3adf256


Try window.onbeforeunload...

Jul 23 '05 #14
>> 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/...8a26e4f3adf256 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

---------------------------------------------------

Jul 23 '05 #15
JRS: In article <11*********************@g47g2000cwa.googlegroups. com>,
dated Fri, 13 May 2005 09:31:46, seen in news:comp.lang.javascript, RobB
<fe******@hotmail.com> 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.
Jul 23 '05 #16

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

Jul 23 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sascha Folville | last post by:
Hi, I'm trying to transform a XML document to PDF using apache (xerces). I want different formatting for first and last page. My code looks like this: <fo:layout-master-set>...
0
by: Betty Harvey | last post by:
The next meeting of the XML Users Group will be held on Wednesday, May 18, 2005 at the American Geophysical Union (AGU) at 2000 Florida Avenue, N.W., Washington, DC 20009-1277. The meeting starts...
3
by: jason | last post by:
I've got this javascript routine (i found on google - thank you) in an asp.net page that on page reload sets the cursor of a textbox to the last line. It works great! Using a similar concept, I...
1
by: Daniel | last post by:
hi, I had an asp:listbox, and everytime i click item inside, the bar automatically go to the top, is there any way to keep the scroll position? I turn on the smartNavigation, it still doesn't...
3
by: tldisbro | last post by:
Hello All, I am trying to use the returned value of the <fo:page-number> element/function in my <xsl:if> test condition. But am unsuccessful in doing so. Is it possible to use it in this fashion...
4
by: freefly_xml | last post by:
I want to test to see if I am on the last page of a document. In this example it is an invoice. I want to print a different table in REGION AFTER when I am on the last page. I have tried many...
2
by: Kevin Burton | last post by:
I don't think I understand the last() function. I have a document that looks like: <Root> <Header>Some text</Header> <Message> <MessageID>1</MessageID> . . . . </Message>
0
by: Betty Harvey | last post by:
NOTE: This is the last meeting of 2006!! The next meeting of the XML Users Group will be held on Wednesday, November 15, 2006 at the American Geophysical Union (AGU) at 2000 Florida Avenue,...
18
by: Mel | last post by:
What is the best method to achieve this (I am relatively new to vb.net)? Should I use an ini file or the registry? Is there another option available in vb.net that is the preferred way? This...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.