0

I want all my webpages to open at a specific scroll point (scrolled to, let's say, 300px).

I don't want this kind of Javascript solution <body onLoad="window.scroll(0, 150)"> because it's really annoying for the users. In effect, it scrolls only when the page is loaded which means sometimes (if the page loads slowly) it might scrolls many seconds after the page is opened, when the user already started reading the content.

I dont want to change my urls, like in this CSS solution mywebsite.com#scrolldiv

Actually the CSS solution mywebsite.com#scrolldiv works like charm, but it changes my urls, which i don't want.

  1. Is there a way to use the Javascript solution and scroll BEFORE the page is loaded and the content is displayed? I don't want the users to see the annoying automatic scrolling.
  2. Is there a way to use the CSS solution without changing the URLS?
Community
  • 1
  • 1
Alberto Fontana
  • 928
  • 1
  • 14
  • 35

2 Answers2

2

this solution works for your requirements, the user won't have to wait for load, and there is no url change required:

https://stackoverflow.com/a/22764622/2423221

Community
  • 1
  • 1
bobbor
  • 141
  • 5
0

Just put the script at the end of the html file. Then it will be called as soon as the dom is ready.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

    <!-- other content

             (__)
             (oo)                                  
       /------\/    
      / |    || 
     *  /\---/\           
        ~~   ~~   

    /other content -->

    <script type="text/javascript">

        window.scrollTo(0,150);

    </script>
</body>
</html>
gang
  • 1,758
  • 2
  • 23
  • 36