Tutorial :Javascript scroll



Question:

How to scroll to top of the page in php with javascript

scroll(0,0); -> this is not working? On click of submit, i call a function to validate the form and incase of error i will be setting the errormessage thru innerhtml and need to scroll to top of the page without submitting.

Everything works...but its not scrolling to top of the page


Solution:1

Use window property scrollTop (MSDN, MDC) to set number of pixels to scroll offset.

window.scrollTop = '0';  


Solution:2

If you have a particular element that you want to scroll over, lets say, the element of whose innerHTML you are modifying then you can use the following

 element.scrollIntoView(true);  

Works in all browsers


Solution:3

How about trying window.scrollTo(0,0). It is basically the same thing as scroll(0,0).

If it does not work, show us more code.


Solution:4

Scrolling to the absolute top of the page can be achieved by either

window.scrollTo(0,0);  

or

window.location = "#";  

Note that the second method will append "#" to the current URL.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »