0

Is there a way to get the position of the page / document (inner window) relative to the browser window?

For instance, there are properties on the window object like innerWidth / outerWidth which allow to get the width of the document renderer and width of the browser window respectively.

I could not find any properties that could determine position of the document within browser window. There's screenX, but it's position of the browser window relative to the screen.

Note that what affects the document position the most is devTools, which can be attached to either left, bottom or right edge of the screen. Also some browsers have sidebars. So it's not as simple as assuming that the docX relative to the window is 0 and docY = outerHeight - innerHeight

Is there any API available to get it?

Edit:

Example: Let's say the browser's tab and addressbar UI at the top is 150px in height and there's a sidebar to the left that has width of 200px. The position would be (200px,150px)

Maciej Krawczyk
  • 14,825
  • 5
  • 55
  • 67
  • i´m not quite sure if u are looking for: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop ? – john Smith Dec 07 '21 at 09:23
  • i re-read ur question and i think you are looking for the viewport-dimensions, maybe this works for u: `height = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)` taken from https://stackoverflow.com/questions/1248081/how-to-get-the-browser-viewport-dimensions – john Smith Dec 07 '21 at 09:28
  • scrollTop is the scroll position. I'm not looking for height either. I'm looking for the position of the document relative either to the browser window or the screen. I'm pretty sure now that it's not possible, but I asked anyway in hope maybe someone knows something I don't. – Maciej Krawczyk Dec 07 '21 at 09:42
  • I.e. let's say the browser's tab and addressbar UI at the top is 150px in height and there's a sidebar to the left that has width of 200px. The position would be (200px,150px) – Maciej Krawczyk Dec 07 '21 at 09:43
  • okay i got it now, what you can do `window.screen.height vs window.innerHeight` and `window.screen.width vs window.innerWidth` (screen includes the sidebars etc.) but u have no clue if its left or right or top or bottom – john Smith Dec 07 '21 at 09:53
  • @John yes. this is insufficient to calculate the position. Even for y-axis, devTools could be attached to the bottom or not. On x-axis, there could be a sidebar, and/or devTools on either side. – Maciej Krawczyk Dec 07 '21 at 10:41

1 Answers1

0

At this point the only solution found so far is using UIAutomation AutomationElement to get the object from the browser and grab top left from that.

I'm looking for a purely javascript implementation to get the browser top level document x,y relative to the screen.

The last version of GetAbsCoordinates in answers here: Get On-Screen Location of ...

Dead Mike
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 22:14