Javascript functions to find mouse coordinates?
Hi,
Are there javascript functions to find the cursor position of a mouse?
Also, are there javascript functions to find if the mouse cursor is within a listbox?
A C# code sample is shown below:
Listbox lb; //lb is a listbox
Point cpos = lb.PointToClient(Cursor.Position); //find coordinates of mouse
if(lb.ClientRectangle.Contains(cpos)) //if mouse cursor is within the listbox
Are there any javascript functions which can do the same as the above c# methods?
[509 byte] By [
ZhiYi] at [2007-11-19 19:50:37]

# 1 Re: Javascript functions to find mouse coordinates?
As far as I know, There are the six property pairs
clientX,clientY
layerX,layerY
offsetX,offsetY
pageX,pageY
screenX,screenY
x,y
The screenX and screenY properties are the only ones that are completely cross–browser compatible. They give the mouse position relative to the entire computer screen of the user.
Example:
function doSomething(e)
{
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY)
{
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY)
{
posx = e.clientX + document.body.scrollLeft;
posy = e.clientY + document.body.scrollTop;
}
// posx and posy contain the mouse position relative to the document
// Do something with this information
}
Hope it helps!