June 2, 2010

how to set focus on top of the page

Question by vakas

How do I set focus to the top of a page?

Answer by Starx

It would be helpful if you write a little so that we can understand your problem.

A very easy way is linking to class or id on a page.

For example this page “page.html”

<body>
        <div id="top"> ..................Content................</div>
        <div id="otherdiv">........................</div>
</body>

then if you need to focus on top, send a link like “page.html#top” the page will automatically focus on top

or if you want to stick to javascript then do this

document.location.href = '#top'; //to send it to the top

But I would say use simple html anchor to go to top like

<a href="#top">Back to Top</a>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!