Catalin Marcu
Catalin Marcu

Reputation: 634

HTML - Auto Jump to Named Anchors

I have a website that has a search box and some options. On the mobile page, this search box and options are visible first, which is really good on the start page. But when user searches something, the result is shown in a page having again the search box and options on top.

I would like to write a piece of code, that automatically jumps to content start. Simply, I have created an anchor using: <a name="contentstart"> just before the content. When the user opens this page, it should automatically jump to contentstart without having to press any link/button. How can I achieve that.

Upvotes: 0

Views: 2717

Answers (2)

Samuel Kelemen
Samuel Kelemen

Reputation: 176

You can simple use id and hash in url.

// some html
<div id="contentstart"></div>
// content

After that if you sufix your url with #contentstart you jump right to constentstart div.

http://webpage.com/something#contentstart

Upvotes: 1

Codeek
Codeek

Reputation: 1624

simply give an id to your a tag,

and use following javascript function :

function jump(id){
    var top = document.getElementById(id).offsetTop; //Getting Y of target element
    window.scrollTo(0, top);                        //Go there.
}​

and in onload of body tag, call the function by passing the id of the anchor tag

Upvotes: 1

Related Questions