How to Get Page Size & Scroll in JavaScript
JavaScript lets you identify the height of a user's window and scroll down the page automatically. The technique should be used sparingly on a Web page, because it does not provide a good user experience when the browser consistently auto-scrolls down the page. You use the window class to detect screen size and scroll through the Web page.
Instructions
-
-
1
Right-click the HTML page you want to edit. Click "Open With" and select the HTML editor you want to use. Click "Open" to open the page in your editor.
-
2
Create a JavaScript script tag in the HTML code. Copy and paste the following script tags to set up a JavaScript section on your page:
<script type="text/javascript"> </script>
-
-
3
Type the following code to retrieve the size of the screen:
var screen_size = window.innerHeight;
The code retrieves the height of the user's window in pixels so that you can scroll to a section within the browser or to the end of the page.
-
4
Type the following code to scroll down the page:
window.scrollBy(0,100)
The code above scrolls down by 100 pixels. The first number is how many pixels to scroll horizontally. The second number sets the window to scroll vertically. You can scroll to the bottom of the page using the screen height retrieved in Step 3. For instance, the following code scrolls to the bottom of the page:
window.scrollBy(0,screen_size)
-
1