2

Possible Duplicate:
Screen Coordinates of a element, via Javascript

So what I need is simple - find out where on users screen (NOT browser window) top left corner of element is?

Community
  • 1
  • 1
Rella
  • 65,003
  • 109
  • 363
  • 636

3 Answers3

5

On the user's screen? You can't do that. I'm sure you could code something up like that with a Java applet or similar, but not with JavaScript.

The closest you can get is by getting the position of the browser window using the window.screenX and window.screenY properties on IE, and the window.screenLeft, etc properties on other browsers, then getting the relative offset of the element using the method described here (from Breezer's answer), and adding the coordinates together. Unfortunately, this doesn't account for toolbars or sidebars in the browser, so this is a really rough estimate. Don't do this if you need it to be 100% accurate.

Sasha Chedygov
  • 127,549
  • 26
  • 102
  • 115
  • Actually, you can. See Breezer's answer. – El Yobo Dec 04 '10 at 10:26
  • 1
    @El Yobo: He said the user's screen, "NOT browser window". Unless I'm misunderstanding the question, you can't figure out the position of the browser window on the screen with JavaScript, so you can't do that. – Sasha Chedygov Dec 04 '10 at 10:28
  • @ElYobo - that answer is incorrect, re-read the question - this one is correct. – Nick Craver Dec 04 '10 at 10:31
  • If you couldn't, how would [this](http://www.chromeexperiments.com/detail/browser-ball/) work? – Félix Saparelli Dec 04 '10 at 10:33
  • I think you can, window.screen.top gives offset of top edge of client area, for example. – Paul Dixon Dec 04 '10 at 10:34
  • @Nick, thanks, you're right, I did read the question incorrectly. Unfortunately SO won't let me change my incorrect downvote unless the question is edited... @musicfreak, please make a minor edit to your answer so that I can revert my vote. – El Yobo Dec 04 '10 at 10:34
  • 1
    @Paul Dixon: Hmm, Chrome says it's undefined. @passcod: I'm not sure, actually, good question. Although I'm fairly certain that has something to do with the fact that it creates its own windows, which JavaScript has more control over. @El Yobo: I've edited, thanks. :) – Sasha Chedygov Dec 04 '10 at 10:37
  • 1
    @Paul - this doesn't work cross-browser, for example in Chrome right now `window.screen.top` is `undefined`...@musicfreak is correct in that you can't do this. The fact that people are downvoting the answer means they didn't read the question at all, or are downvoting because they *assume* you can do this, but don't know how (why haven't *they* posted an answer?)...either way they're wrong. – Nick Craver Dec 04 '10 at 10:38
  • @Paul - it appears that there are portability issues with this, see http://www.w3schools.com/jsref/prop_win_screenleft.asp – El Yobo Dec 04 '10 at 10:39
  • It's an interesting question, am digging around but reaching the same conclusion. Seems possible with Firefox, but making something portable looks....hard :) – Paul Dixon Dec 04 '10 at 10:45
  • 1
    Actually, to be clear, that's FF3.6 - it's this property that looks promising there: https://developer.mozilla.org/en/DOM/window.mozInnerScreenX – Paul Dixon Dec 04 '10 at 10:50
3

if you use jquery you can use http://api.jquery.com/position/ other wise you have to use offset read more about it here http://www.quirksmode.org/js/findpos.html

Breezer
  • 10,410
  • 6
  • 29
  • 50
  • This finds it in the **browser window**, not the **screen** ...explicitly what the question asked *not* to find. – Nick Craver Dec 04 '10 at 10:31
  • If you know the position in the window, you can get the window offset from window.screen, surely? – Paul Dixon Dec 04 '10 at 10:33
  • @Paul - Nope, even then you don't know the toolbar size, etc. and you can't get the position to begin with. – Nick Craver Dec 04 '10 at 10:35
  • But combining that with window.screenX and window.screenY it should get what he needs. – AlfonsoML Dec 04 '10 at 10:36
  • 1
    @AlfonsoML - that gives the upper left corner of the **entire** window, not the page area, which means you still have to account for toolhar height, etc...which you don't know. – Nick Craver Dec 04 '10 at 10:39
  • Indeed, see http://stackoverflow.com/questions/504052/determining-position-of-the-browser-window-in-javascript – Paul Dixon Dec 04 '10 at 10:40
0

It seems that this is possible after all; see this w3schools item (IE) and this other w3schools (Chrome and FF). I haven't tested this myself, however.

On browsers except Chrome and FF, use window.screenLeft and window.screenTop. On Chrome and FF use window.screenX and window.screenY (I have tested this in Chrome; it works there).

El Yobo
  • 14,823
  • 5
  • 60
  • 78
  • 1
    Your link clearly shows this *doesn't* work in Chrome or Firefox. For the second, it's not correct, again this gives the **window** (the entire window, not page area) upper left corner - you still don't know how far between that and the page area. – Nick Craver Dec 04 '10 at 10:43
  • @Nick - I have two links. The first one also says which values to use for FF and Chrome. – El Yobo Dec 04 '10 at 10:44
  • @ElYobo - Please see my comments on the other answer, this isn't correct. Not my downvote...but this was already discussed in comments on the question already :) – Nick Craver Dec 04 '10 at 10:47
  • @Nick, yes, you're right again, didn't think about the other bits inside the screen. I did mention the portability issues in the comments above - but the Chrome + FF stuff wasn't in there and I had thought that was sufficient... not so, unfortunately. – El Yobo Dec 04 '10 at 11:22
  • Although, if one opened a window using javascript and prevented any window decorations, this *would* be sufficient for that window, yes? – El Yobo Dec 04 '10 at 11:26
  • @El Yobo: Not exactly. What about the title bar? And some browsers (Firefox) let you override the disabling of window decorations, so in those cases it wouldn't work, either. As been said multiple times, you can get a pretty close *guess* but you can never be *exact* about this. – Sasha Chedygov Dec 05 '10 at 21:47
  • So it seems; glad to see that your correct answer has eventually floated to the top :) – El Yobo Dec 05 '10 at 22:27