0

How to determine current mouse cursor coordinates using only native JS? I know that we can use jQuery mousemove, but I think that it's bad idea to include the jQuery for handling a mousemove event only.

2 Answers2

0

Pretty much the same way as in jQuery

window.addEventListener('mousemove', function(e) {
    document.body.innerHTML = 'position : ' + e.pageX + ' x ' + e.pageY;
}, false);

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
0
window.addEventListener('mousemove', function(e) {
    alert('position : ' + e.pageX + ' x ' + e.pageY)
}, false);
Chris Brickhouse
  • 650
  • 5
  • 15
  • You might also want to check if e is set. As I recall older IE Browsers have a global event object, so first line in the function could be something like `e = e || event` – beipawel Feb 05 '14 at 16:32
  • @beipawel - older IE doesn't support addEventListener, so not really an issue. Good copy of the answer BTW. – adeneo Feb 05 '14 at 16:35