Absolute position of an element using Javascript

|
In a web browser window, elements are placed according to their style and the style and the position of their parent elements. Margin, padding, floating, relative position, static position, fixed position, borders,... can affect the position of an element in the browser window. In Javascript, it is possible to get the absolute position of any element. You just need to pass an DOM element (obtain with getElementById or whatever...) and you get the result as a Javascript object.

function findXY(obj) {
if (obj==null) return {x:0,y:0};
var coords=findXY(obj.offsetParent);
return {x:obj.offsetLeft-obj.scrollLeft+coords.x,y:obj.offsetTop-obj.scrollTop+coords.y};
}

var coords=findXY(document.getElementById("myId"));

The values coords.x and coords.y gives you the position of the DOM element from the top left corner of your web page.

This function is used to display sprite in this Ajax/javascript online Game. Check the javascript source code in the web page to see how it works.

How to get the absolute position of an element using javascript, Absolute positionning in javascript, Javascript element coordinates, Absolute screen position in javascript