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?
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>
).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With