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.
Asked
Active
Viewed 764 times
0
-
Coordinates relative to what? The page, a container? – miguelcobain Feb 05 '14 at 16:25
-
jQuery mousemove is just an interface to the standard Javascript mousemove event. – Barmar Feb 05 '14 at 16:27
2 Answers
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