ASD
ASD

Reputation: 4967

How can I scroll to the top of a page using Javascript?

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

Upvotes: 0

Views: 7808

Answers (5)

manishnitte
manishnitte

Reputation: 211

<a href="#">Scroll to Selected Position</a>

Above code worked for me. I tried with onclick="window.scrollTo(0, 0);" but this does not worked for me, all I needed was href="#" and no onclick. We might also notice that # will be getting appended in url.

Old non-working code has href="javascript:scroll(0,0)" which had an issue in doing scroll to top in angular/iframe component.

Above code does not worked in Firefox.

Final Solution:

function scrollToSelectedPosition() {
   var sectionToJump = document.getElementById('section-to-jump');
   $(sectionToJump).scrollIntoView();
}

and

<a onclick="scrollToSelectedPosition()">Scroll to Selected Position</a>

This worked in FireFox, Chrome, Safari and IE.

Upvotes: 0

jmishra
jmishra

Reputation: 2081

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

Upvotes: 3

Justin Johnson
Justin Johnson

Reputation: 31300

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.

Upvotes: 1

nemisj
nemisj

Reputation: 12000

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

window.scrollTop = '0';

Upvotes: 5

epascarello
epascarello

Reputation: 207537

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.

Upvotes: 0

Related Questions