`window.pageYOffset` is wrongly reported as `0` until well after window load event when the browser itself has restored scroll position, such as from back button or reload

By design Issue #8605539

Details

Author
Dennis H.
Created
Aug 23, 2016
Privacy
This issue is public.
Reports
Reported by 1 person

Sign in to watch or report this issue.

Steps to reproduce

I created a minimal test case:

https://dennishall1.github.io/pageYOffset-test/

Just scroll down a bit and then hit refresh. The test will tell you if it passes or fails.

Close and reopen your browser, or just clear cache, to try it again (it uses sessionStorage).

Attachments

0 attachments

    Comments and activity

    • Happens in IE9+, including Edge.

    • Microsoft Edge Team

      Changed Assigned To to “Travis L.”

      Changed Assigned To to “Bogdan B.”

      Changed Status to “By design”

    • The way this works is we wait until the page has finished parsing before restoring the scroll position. The script you are in can still rewrite the document and cause trouble. Can you point out a real website where this is an issue? I think our design is perfectly valid, but real website having issues could maybe convince me otherwise.

    • Parallaxing websites. Websites that do things with content based on scroll position. There are plenty of valid use-cases. Unfortunately, IE reports pageYOffset as 0 all the way up to, including, and just after the window load event. Chrome, Firefox, and Safari correctly report the window scroll position at all times. Because IE doesn’t report the correct value until very late in the page render flow, I have to put a setTimeout inside of the window load event, and users in IE experience a jump, and generally, correctly, believe that their browser is the culprit.

    • Real-world CODE:

          if(msie){
              // ie...
              window.addEventListener('load', () => {
                  window.setTimeout(() => {
                      this.accountForInitialPageScroll();
                  }, 500);
              })
          } else {
              // everyone else
              this.accountForInitialPageScroll();
          }

    You need to sign in to your Microsoft account to add a comment.

    Sign in