Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump to a element automatically on page load

i have elements like this on page

<a href="#hi">go to hi</a>
.
.
<a name="hi">hi text here</a>

but i would like users go to "hi text here" at first on page loading. how to do this?

like image 607
user006779 Avatar asked Dec 10 '22 05:12

user006779


2 Answers

I'd suggest that you first test for another hash before moving your users' browser to focus another element:

if (!document.location.hash){
    document.location.hash = 'hi';
}

JS Fiddle demo.

Incidentally, you can use the hash (the part after the # in the URL) to jump to any element that has an id, you don't need to use the named-anchors (<a name="hi">...</a>).

like image 155
David Thomas Avatar answered Dec 30 '22 19:12

David Thomas


Either, you can use the URL with the anchor (mysite.com/#hi) or you could use javascript:

document.getElementById('hi').scrollIntoView(true);

Please note that you should use ID, not name.

like image 41
Marcus Olsson Avatar answered Dec 30 '22 18:12

Marcus Olsson