40

For various silly reasons, I'd like to be able to detect the rectangle of the browser window on screen. Title bar and all.

Is this possible, or is JavaScript limited to the view port of its page?

Edit: I may have been unclear, but view port is the portion of the page that's visible in the window. This may not be a term commonly used with browsers, but it's common in graphics.

animuson
  • 53,861
  • 28
  • 137
  • 147
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238

3 Answers3

61

For standards compliant browsers:

X - window.screenX
Y - window.screenY

For IE:

X - window.screenLeft
Y - window.screenTop

Keep in mind, implementations vary. Beware of multi-monitor setups...

John
  • 1
  • 13
  • 98
  • 177
Shog9
  • 156,901
  • 35
  • 231
  • 235
  • 5
    I just tried this today, and screenLeft and screenTop works in IE 8, but screenX and screenY no longer works. Seems IE finally decided to cooperate on this one. Anyone else have the same results? – trusktr Sep 29 '11 at 03:18
  • This is not a good answer. What about multi-monitor setups. This does not work. – Marco Wagner Sep 19 '16 at 11:25
  • 1
    Define "work", @Marco. There's no awareness of multiple monitors, but it should function correctly in terms of returning coordinates that can be used for positioning a window within the virtual "screen coordinate system" that's mapped to the monitors. Just don't try to be "clever" - the information is sufficient for positioning, but using it to make assumptions about what the user can see - or wants to see - is dangerous. These are not a substitute for proper units of measure and responsive design. – Shog9 Sep 19 '16 at 16:33
  • MDN [says](https://developer.mozilla.org/docs/Web/API/Window/screenLeft): "Note: `screenLeft` is an alias of the older [`Window.screenX`](https://developer.mozilla.org/docs/Web/API/Window/screenX) property. `screenLeft` was originally supported only in IE but was introduced everywhere due to popularity." – Spooky Feb 01 '19 at 03:13
9

Take a look at the following properties:

(These won't give you the window size or position, but may allow you to correctly interpret them when operating on a system with multiple monitors)

Shog9
  • 156,901
  • 35
  • 231
  • 235
matt b
  • 138,234
  • 66
  • 282
  • 345
-8

Copy pasted from google first result:

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() 
{
return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?  document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 
function pageHeight() 
{
return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 
function posLeft() 
{
return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
} 
function posTop() 
{
return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
} 
function posRight() 
{
return posLeft()+pageWidth();
} 
function posBottom() 
{
return posTop()+pageHeight();
}
Saiyine
  • 1,579
  • 3
  • 15
  • 27
  • From the same page: "To be able to determine the available space within the browser window and the current position of that window within the current web page, you need to copy the following script and attach it to the head section of your page." I think this means viewport. – Allain Lalonde Feb 02 '09 at 17:34
  • Double checked, and this does not do what I want. When my Window is in the middle of the screen, alert(posLeft()) returns 0. – Allain Lalonde Feb 02 '09 at 17:38
  • pageXOffset returns "Returns the number of pixels that the document has already been scrolled horizontally." - not the offset on the screen – matt b Feb 02 '09 at 17:50
  • 1
    This does not do what the poster asked. – Tyler Biscoe Sep 11 '12 at 14:51
  • great example of why StackOverflow is great... the previous top Google result had old, ugly, bad code. Now, this SO question is the top Google result, and it has a correct accepted answer at the top – Kip Feb 03 '14 at 19:42