What is the best way to restore the scroll position in an HTML document after the screen has been rotated? (This is in a Cocoa Touch UIWebView, but I think it’s a problem everywhere.) The default behavior seems to restore the y-offset in pixels, but since the text has been reflowed this is now a different position in the document.
My initial thought is to:
- Pepper the document with invisible, uniquely-id’ed elements.
- Before rotation, search for the element e whose y-offset is closest to the scroll offset.
- After rotation, update the scroll offset to e‘s new y-offset.
Even if that works, I’d prefer not to insert a bunch of crud into the document. Is there a better way?
Here’s a diagram to clarify the problem. Restoring the original y-offset does not produce the intended result because more text fits on a line in landscape mode.

Not pretty but it works. This requires there to be span tags throughout the document text.
After rotation,
document.getElementById(spanId).offsetTopis the new y-offset, wherespanIdis the result offindClosestLocator()before rotation.