Geeting the relative coordinates of a click

If you want to get the coordinates of a click event relative to the object it happened to, the following event handler works fine on all borwsers provided that the object counts as a layer (offset container). To achieve this, use position:relative or absolute on the object.
funtion(e){
var mx=0;
var my=0;
if(window.event){
mx=window.event.offsetX;
my=window.event.offsetY;
}else{
mx=e.layerX;
my=e.layerY;
}
}

To get the global coordinates, see http://www.quirksmode.org/js/events_properties.html#position

Popular Posts