My question
What JavaScript code will tell me where the viewport of the browser is located relative to the screen?
Context
My web application includes an applet that allows taking a snapshot via java.awt.Robot (the applet’s jar is of course signed and is privileged to perform this).
The problem is that Robot’s createScreenCapture works with rectangles relative to the entire screen, whereas I want to capture a rectangle relative to the viewport.
A browser can obviously be anywhere on the screen, but even if it is maximized (and therefore begins at the top left of the screen, i.e. {0,0}) I still don’t know how much the content is pushed down because of the window header or some toolbars.
My research so far
It seems only IE gives the viewport position through window.screenTop/Left.
Chrome supports these, but they hold the browser position.
FF doesn’t support these, instead it has screenX/Y, but like Chrome they hold the browser position.
Making sure we all use the same terminology
Screen – AKA the desktop. for example I have a WSXGA+ (1680×1050) display. I use Windows and my taskbar is always shown at the bottom so it consumes about 50 pixels vertically.
Browser – a window that may or may not have various toolbars: address and/or bookmarks bar at the top, status/add-on bars at the bottom, etc.
Viewport – where a URL is actually being rendered.
Your applet knows its location via getLocationOnScreen()
Here is a Java applet that prints the screen location of the mouse cursor while you are inside of it:
ScreenTest.java:
screentest.html:
Here’s a simpler example called by javascript. The applet is 1px by 1px in the upper left corner of the page.
ScreenTest2.java:
screentest2.html: