Getting the current mouse position
jQuery $.mousedown(), $.click(), $.mouse...
2015 page
This is the original page from 2015, kept at its address while the tested rewrite is in progress. The code is of its time: it ran in Internet Explorer and does not use what browsers have gained since. The rewrite replaces it here.
In short
How to get the current mouse position on mouse move or click.
To get the current position of the mouse, attaching an event handler to any mouse action is required. The mouse’s position is reported on the event object received by the handler function.
// event handler function
function handler(e) {
e = e || window.event;
var pageX = e.pageX;
var pageY = e.pageY;
// IE 8
if (pageX === undefined) {
pageX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
pageY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
console.log(pageX, pageY);
}
// attach handler to the click event of the document
if (document.attachEvent) document.attachEvent('onclick', handler);
else document.addEventListener('click', handler);Read Jack Moore’s well written article about cross-browser mouse positioning to learn more details and options about this topic.