I have a page where a search field is placed in the header. This input uses the autofocus
attribute to automatically place the focus into the field so that users can start searching right away:
<html>
<head><title>autofocus issue</title></head>
<body>
<header>
<form method="post">
<input name="search" autofocus />
</form>
</header>
<article>
<p>Lots of contents here, so that the next link
can only be reached by scrolling down ...</p>
<a href="http://link.to/some/page">Go somewhere</a>
</article>
</body>
</html>
While this works fine there is a problem when going forth and back in history. If the user scrolls down my page and clicks on a link and then goes back again (using browser history), my original page scrolls to the top where the search input is located.
This is a usability flaw because the scroll position shouldn't change when going back in history. How do I avoid this?