It seems like there are two potential issues at play here, but without more information, it's hard to pinpoint the exact problem.
Potential Issue #1: HTML Anchor Links
If your header has an ID, such as
<h1 id="myinfo">Contact</h1>
, and the URL includes something like
example.com/index.html#myinfo
, the browser will automatically scroll to that anchor when the page loads.
Potential Issue #2:
When refreshing a webpage, most browsers will remember the previous scroll position. So, if you were at the "Contact" section and refreshed the page, it would return you to that same position.
Possible Solution:
If you want to ensure the page always scrolls to the top on refresh or anchor use, you can try adding the following script just before the closing BODY
tag or in the HEAD
section:
<script type="text/javascript">
window.onload = function() {
window.setTimeout(
function() { window.scrollTo(0,0); },
10
);
};
</script>
It's important to use window.setTimeout
to ensure the scrolling happens after the page is refreshed.
I hope this information proves helpful in addressing your concerns.
UPDATE: 2018/Nov/16
Note that the script will not work if HTML anchor links are essential for navigating within the page. In this case, anchor links will be disregarded.
If JavaScript is disabled, the provided solution will not function properly. In that scenario, you could implement a script to hide the page before content loads and then reveal it afterward. However, this may result in a brief white screen upon page loading, similar to how Google used to handle page loading.
Ensure that the script is placed after the BODY
tag but before the main content for optimal performance. Placing it in the HEAD
section will not yield the desired outcome.
<body>
<script type="text/javascript">
<!--
document.body.style.display = "none";
// If onbeforeunload fails
window.onload = function() {
window.setTimeout(
function() {
window.scrollTo(0,0);
document.body.style.display = "inherit";
},
0
);
};
/* This should scroll the page to the top before the page refreshes */
window.onbeforeunload = function() { window.scrollTo(0,0); }
//-->
</script>
. . . { Your content here } . . .
Keep in mind that if JavaScript is disabled, this solution will not be effective since some users may have JavaScript turned off. Unfortunately, there is no alternative method to consistently force the page to scroll to the top.