Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(common): Prefer to use pageXOffset / pageYOffset instance of scro…
…llX / scrollY

This fix ensures a better cross-browser compatibility.
This fix has been used for angular.io.
  • Loading branch information
wKoza committed Nov 15, 2020
commit a20fec17265522109a17f56615051dcfcf8845ed
2 changes: 1 addition & 1 deletion aio/src/app/shared/scroll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ScrollService implements OnDestroy {
poppedStateScrollPosition: ScrollPosition|null = null;
// Whether the browser supports the necessary features for manual scroll restoration.
supportManualScrollRestoration: boolean = !!window && ('scrollTo' in window) &&
('scrollX' in window) && ('scrollY' in window) && isScrollRestorationWritable();
('pageXOffset' in window) && isScrollRestorationWritable();

// Offset from the top of the document to bottom of any static elements
// at the top (e.g. toolbar) + some margin
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/viewport_scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class BrowserViewportScroller implements ViewportScroller {
*/
getScrollPosition(): [number, number] {
if (this.supportsScrolling()) {
return [this.window.scrollX, this.window.scrollY];
return [this.window.pageXOffset, this.window.pageYOffset];
} else {
return [0, 0];
}
Expand Down Expand Up @@ -149,7 +149,7 @@ export class BrowserViewportScroller implements ViewportScroller {
*/
private supportScrollRestoration(): boolean {
try {
if (!this.window || !this.window.scrollTo) {
if (!this.supportsScrolling()) {
return false;
}
// The `scrollRestoration` property could be on the `history` instance or its prototype.
Expand All @@ -166,7 +166,7 @@ export class BrowserViewportScroller implements ViewportScroller {

private supportsScrolling(): boolean {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's really any value in this anymore. I wouldn't remove it at this point, but it appears that pageXOffset and scrollTo is supported in every browser that we support...

@wKoza wKoza Nov 15, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scrollTo doesn't seem to work with IE 11 that is supported by Angular.

try {
return !!this.window.scrollTo;
return !!this.window && !!this.window.scrollTo && 'pageXOffset' in this.window;
} catch {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/common/test/viewport_scroller_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('BrowserViewportScroller', () => {
let windowSpy: any;

beforeEach(() => {
windowSpy = jasmine.createSpyObj('window', ['history', 'scrollTo']);
windowSpy =
jasmine.createSpyObj('window', ['history', 'scrollTo', 'pageXOffset', 'pageYOffset']);
windowSpy.history.scrollRestoration = 'auto';
documentSpy = jasmine.createSpyObj('document', ['getElementById', 'getElementsByName']);
scroller = new BrowserViewportScroller(documentSpy, windowSpy, null!);
Expand Down
3 changes: 2 additions & 1 deletion packages/router/test/bootstrap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ describe('bootstrap', () => {
window.scrollTo(0, 5000);

// IE 11 uses non-standard pageYOffset instead of scrollY
Comment thread
wKoza marked this conversation as resolved.
Outdated
const getScrollY = () => window.scrollY !== undefined ? window.scrollY : window.pageYOffset;
// For cross-browser compatibility, prefer window.pageYOffset instead of window.scrollY
const getScrollY = () => window.pageYOffset;

await router.navigateByUrl('/fail');
expect(getScrollY()).toEqual(5000);
Expand Down