LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Setting scroll position in Internet Explorer (https://www.linuxquestions.org/questions/programming-9/setting-scroll-position-in-internet-explorer-532624/)

jlinkels 02-26-2007 01:42 PM

Setting scroll position in Internet Explorer
 
What I feared had happened. I developed a web page amd tested it in Opera, and it doesn't work in IE6.

This is the code snippet:

Code:

<script type="text/javascript">
  scrollTo(0,344);
  alert ('current position after scroll: ' + document.body.scrollTop);
</script>

Now obviously the page does NOT scroll to y position 344. In the last line, the alert shows the current and correct scroll position.

In other words, retrieving the scroll position works fine, but setting of the scroll position does not.

Alternatively I tried this code:

Code:

<script type="text/javascript">
  document.body.scrollTop = 344;
  alert ('current position after scroll: ' + document.body.scrollTop);
</script>

But that didn't work either.

As a matter of fact, there seems to be no relationship between what I put the scroll position to, and the real position after the call.

These are a number of requested positions, and the real position the scroll position moves to:

Code:

requested  actual
230          203
324          135
523          94

Now I don't see any logic here.

In Opera it works perfectly. In Firefox it does not, but that is due to a (documented) different way of specifying the scroll position.

If anyone ever succesfully scrolled the window in IE to a predetermined position, please let me know!

jlinkels

Cliffster 02-26-2007 06:57 PM

Not exactly an answer to that, but...

I'd try and have it jump to a named anchor in the page, which is more or less the same result.

jlinkels 02-27-2007 05:07 AM

Yes, I know that technique as well.

I am afraid it is not fine grained enough. The application is running on an intranet, with a dozen or more controls. Whenever a control is changed, the contents is written to MySQL, and the page is refreshed.

Since this process is fast, it should look like the page is remaining in place. That is hardly possible with bookmarks. I agree that the user sees the page again in more or less the same position, and that would be satisfactory if the page is away for 2 seconds or so while refreshed.

In the mean time I have made some progress.

If I put the scrollTo(x,y) statement in the <body onload="blah blah"> tag, it works in IE as well. Not as beautiful as in Opera though, first the entire page is loaded and THEN it scroll to the desired position, which is not nice to the eye. (Think about that, it is in the onload statement...) In Opera it remain satisfactory.

Now I am beginning to understand why so often web sites don't in Opera which do in IE. It is IE not complying to the specifications forcing developer to write eeky code, not the other way around.

jlinkels


All times are GMT -5. The time now is 02:03 PM.