File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes How to make a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to make a "back to top" feature?" Watch "How to make a "back to top" feature?" New topic

How to make a "back to top" feature?

Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 47
Hi All,
I have a log page and I want to add a back to top feature, so user can go back to the top of the page.
Could somebody tell me how to do this?
Thanks a lot.

Enjoy life!
Marc Kruze

Joined: Oct 28, 2010
Posts: 5
You could use anchor tags.

I solved this one by using the following code (may vary according to your requirements; this worked for what I had to do):

//This goes on the header
<script language="JavaScript">
function setFocus(componentId) {
var obj = document.getElementById(componentId);
if (obj != null) {
obj.focus(); }

Then on the onclick attribute make a call to setFocus passing the component ID.

I hope this helps.
Jay pratt

Joined: Oct 29, 2010
Posts: 2
There are two ways that I know of.
1) use an anchor tag in the page:
Link: <a href="#top">back to top</a>
Anchor tag placed at the top so the browser knows where to go: <a name="top"></a>

2) You can do it with Javascript: window.scrollTo(0,0)
I agree. Here's the link:
subject: How to make a "back to top" feature?
It's not a secret anymore!