View Full Version : Auto Refresh - Stop Page Scrolling

04-15-2010, 05:38 AM
I'm attaching my php test file but this is a JS problem. When I scroll down in the web page and the meta refresh hits, in Windows Safari and IE6/7/8 browsers, the web page re-positions back to the top. In Opera and FF the page refreshes but it stays where it is.

Can someone look at my test script and see why it is not working in IE and Win Safari? My goal is to have the web page not re-position to the top on the auto refresh.


04-15-2010, 05:53 AM
It's actually just a matter of what each browser does. Firefox is nice in that it will reposition the scroll to where you last were, but IE just doesn't have that implemented. Try removing your script altogether and see if Firefox and Opera still reposition (they probably will).

For your script to actually have a chance of working though, you need to wait for the entire document to load first before trying to set the scroll value. Right now, there is nothing on the document when you set the scroll values, so the browser is automatically setting them back to 0 (as it will not let you scroll to what is not there). You can try wrapping your "scroll reset" code in a window onload handler. Ex:

window.onload = function() {
// set scroll values here.
// this function will run when the page has finished loading

Why do you need a meta refresh anyway by the way? Is the thing that gets updated something that can be updated via AJAX? Using AJAX would be a lot more reliable (and a lot less hacky) than setting cookies and attempting to re-scroll the document to the correct position. Not to mention, the user will see this refresh/scroll happen every so often when they could be in the middle of reading something, and that could be very annoying.

If you're not sure of what you're trying to do can be done with AJAX, then post either your page, or a link to your page and I can let you know if it's possible or not.


04-15-2010, 06:01 AM
Yes Opera and FF work without that code.

I fear there is too much data and formatting, etc. for this to work with Ajax.

The form fetches messaging records and displays them on the page - sort of like email so I want to refresh the page so it grabs the latest messages.

Not sure what you mean by "...Right now, there is nothing on the document when you set the scroll values..."

When I display that test page I can scroll down and wait for the refresh and in Opera and FF the page stays put. In IE and Win Safari it resets to the top.

I'll try our window.onload() suggestion and see if I can make it work that way.


04-15-2010, 06:32 AM
What I mean by "there is nothing in the document when you set the scroll values", I mean that the browser processes the page from the top to the bottom. So with your current code, the browser will create the start <html> tag, create the start <head> tag, and then start running your script. By the time the browser executes the line document.body.scrollTop = posy, the only two things in the document are the start <html> and start <head> tags (i.e. there is not yet even a <body> tag, or any of the content within it that would cause scroll bars to appear). Try doing alert( document.body.scrollTop ); right after you set it. I bet it will alert 0.

By using a window onload handler, you wait until all html elements have been created on the page before running the code in that function. That might give your script a chance to set the correct scroll value.


04-17-2010, 10:14 PM
Hey, any luck on this?