Detecting when the user has gone back a page

O

Oenone

We have a little piece of JavaScript in one of our client's application
which automatically sets the input focus to the first control on the HTML
form.

This works very nicely, except for in one circumstance. If the user is
partway through entering data into the form, clicks a hyperlink to go to
another page, and then clicks the Back button to return to the edit page,
the focus is again reset to the first field on the page. Under this
circumstance I would like the focus to be left wherever the user has
previously put it.

Simply put, I want to only set the focus if the user hasn't already set it
during a previous access to this page.

I can do this in IE by querying the document.activeElement.tagName property.
If this returns BODY, then I know that the focus is in its default state and
can set it appropriately. But Firefox doesn't appear to support the
document.activeElement property, so I can't use this mechanism here.

Can anyone suggest a way to do this that will work in Firefox? The ideas I
have which may provide a solution are:

- see if the focus is set to the default location on the page (whatever that
is) -- how would I do this? I'm not sure how to determine what currently has
the focus in Firefox.

- see if there are pages in the history buffer ahead of the current page
(i.e., that would be reached by clicking the Forward button in the
browser) -- how would I do this?

If anyone could answer either of those questions, or suggest another way I
may be able to achieve my goal, I'd be very grateful.

Thanks in advance,
 
B

BootNic

Oenone said:
We have a little piece of JavaScript in one of our client's
application which automatically sets the input focus to the first
control on the HTML form.

This works very nicely, except for in one circumstance. If the user
is partway through entering data into the form, clicks a hyperlink
to go to another page, and then clicks the Back button to return to
the edit page, the focus is again reset to the first field on the
page. Under this circumstance I would like the focus to be left
wherever the user has previously put it.

Simply put, I want to only set the focus if the user hasn't already
set it during a previous access to this page.

I can do this in IE by querying the document.activeElement.tagName
property. If this returns BODY, then I know that the focus is in
its default state and can set it appropriately. But Firefox doesn't
appear to support the document.activeElement property, so I can't
use this mechanism here.

Can anyone suggest a way to do this that will work in Firefox? The
ideas I have which may provide a solution are:

- see if the focus is set to the default location on the page
(whatever that is) -- how would I do this? I'm not sure how to
determine what currently has the focus in Firefox.

- see if there are pages in the history buffer ahead of the current
page (i.e., that would be reached by clicking the Forward button in
the browser) -- how would I do this?

If anyone could answer either of those questions, or suggest
another way I may be able to achieve my goal, I'd be very grateful.

Thanks in advance,

Use a hidden input to keep track of the elements onfocus.
<input id="tracker" type="hidden" disabled="true">

In each one of your form elements use the onfocus and set tracker value to the id
<input type="text" id="t1" name="address" onfocus="/* set tracker value to this id */">

Your onload can check to see if the hidden tracker element has a value, if it does you can set
focus to that id, if it does not you can set focus to an element of your choice.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top