How to get mouse pointer coordinates with JavaScript?
July 12, 08 by the programmerSometimes we need to get the position of the mouse cursor if we want to make some advanced JavaScripts.
The following JavaScript does exactly that, it takes the coordinates of the mouse pointer.
Check out the JavaScript in action
| X: | 0 |
| Y: | 0 |
function get_mouse_pointer_coordinates(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 + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
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 + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
//You have the coordinates in the posx and posY variables
//You can do whatever you want with them after this point
}

computersoftwarevirusDum Says: 02.08.08 at 5:16 pm
The site minanov.com is amazing resource.
Good job, owner.