-2

On this page:

http://alien.devprose.com/screenfad

I'm attempting to have it scroll to a specific position using javascript when the page is loaded. For example purposes I have this code in the head:

<script type="text/javascript">

window.scrollTo(300,300);

</script>

However, nothing is happening. Any ideas?

Jon Lachonis
  • 911
  • 1
  • 8
  • 18

2 Answers2

1

It appears that you have jQuery, so the code should be in $(document).ready() like so:

 <script type="text/javascript">
 $(document).ready(function() { window.scrollTo(300,300); });
 </script>

This way, when the window is done loading it will scroll.

SameOldNick
  • 2,397
  • 24
  • 33
0

You can't call window.scrollTo() until after the document has loaded. Scripts in the HTML (including both in the head and in the body) are executed before that. This StackOverflow question should explain the best ways to run scripts after it's finished loading. In summary:

window.onload = function() { window.scrollTo(300,300); };
Community
  • 1
  • 1
Chase Sandmann
  • 4,795
  • 3
  • 30
  • 42