Adblock Plus and (a little) more

Getting screen coordinates for an HTML element · 2010-01-31 02:27 by Wladimir Palant

Yes, getBoxObjectFor() is deprecated, we all know that. And there is getBoundingClientRect() now which is much better anyway. But what should I do if I need the screen coordinates of an HTML element? getBoundingClientRect() won’t provide them and translating doesn’t seem possible (window.screenX is not the screen position of the window’s client area). Google found a newsgroup discussion on that topic but I already knew that popups can be positioned relative to a node automatically. I need to update the position of a popup that is already open and there doesn’t seem a way to realign the popup with its anchor node without closing it (at least not in Firefox 3.5).

What I found out is that accessibility API can also be used to determine the screen position of a node. You get the nsIAccessibleRetrieval service, call getAccessibleFor() with your node and then getBounds() on the resulting nsIAccessible. How reliable is that? I have no idea. Will this work on all platforms? Which nodes will have an nsIAccessible representation? Can we have a supported API to replace getBoxObjectFor()? Questions over questions…

Tags:

Comment [2]

  1. Boris · 2010-01-31 06:37 · #

    Screen coordinates in what units, and why do you need them, really? Fundamentally, screen units are broken in all sorts of ways (broken with multiple screens, for example!) and should be avoided if at all possible.

    Note that subtracting the getBoundingClientRect position of the root from that of the element you really care about should give you the viewport-relative position, which you can then add to window.screenX/screenY.

    I can’t answer most of your accessibility api questions, but I can tell you that the moment you do that your DOM performance will go down the tubes (accessibility code is O(N^3) or so in the number of consecutive DOM mutations).

    > Can we have a supported API to replace getBoxObjectFor()?

    Wouldn’t a better api for positioning popups work better for what you want?

    Reply from Wladimir Palant:

    For my use case, popup.moveTo(-1, -1) should do. Unfortunately, this is only supported starting with Firefox 3.6 and undocumented. Still, given that popup.moveTo() expects screen coordinates – it would be nice to get them from somewhere for the more complicated use cases where the default behavior won’t do. I don’t think that the popup will change screens after opening so the API Robert mentions should do indeed.

  2. Robert O'Callahan · 2010-01-31 09:57 · #

    Use nsIDOMWindowUtils::screenPixelsPerCSSPixel and window.mozInnerScreenX/Y.

    Reply from Wladimir Palant:

    Thank you, that looks good – I missed the introduction of these properties somehow and didn’t find them in source code either. My immediate problem are Firefox 3.0/3.5 where this won’t help but good to know that popup.moveTo() API is actually usable now.

Commenting is closed for this article.