Created
October 31, 2013 15:15
-
-
Save ondrek/7251430 to your computer and use it in GitHub Desktop.
How to get mouse position in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onmousemove = logMouseMove; | |
function logMouseMove(event) { | |
e = e || window.event; | |
mousePos = { x: e.clientX, y: e.clientY }; | |
console.log(mousePos); | |
} |
That is so strange. When I execute window.event
, it says undefined
, however when I execute your code, everything work like charm.
I love this community.
@kingname, I think your error lies in executing regardless. His code is e = event || window.event
so, even if the window.event
is undefined, the code still works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@laras126 There is error but at line 3 - insted "event" as an argument should be "e" (with "event" there is an error "e is not defined"), which is being passed further. In that case you don't have to declarate later "let" or "var".
Have a nice day! :)