With pure JavaScript, you can do this:
Page URL:
meusite.com.br/#localdesejado
or
meusite.com.br/index.html#localdesejado
or
meusite.com.br/index.html?localdesejado
Script:
(function(){
url_ = location.href;
if(url_.match(/localdesejado/)){
setTimeout("window.scrollTo(0,document.body.scrollHeight/2)",1);
}
})();
The script checks to see if the "localhost" string passed in the URL (eg, meusite.com.br/#localdesejado
) is present and scroll
to half the screen.
I used the " localhost " parameter as an example only. You can use the term you want in the script.
Why did I use setTimeout
?
Because some browsers (do not know if everyone, maybe all) will automatically return to the top after window.scrollTo
. Using setTimeout
, even with a minimum value of 1
, this problem with the browser is bypassed and the screen does not return to the top.