Getting screen coordinates for an HTML element · 2010-01-31 02:27 by Wladimir Palant
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…
Commenting is closed for this article.