1var pointerX = -1;
2var pointerY = -1;
3document.onmousemove = function(event) {
4 pointerX = event.pageX;
5 pointerY = event.pageY;
6}
7setInterval(pointerCheck, 1000);
8function pointerCheck() {
9 console.log('Cursor at: '+pointerX+', '+pointerY);
10}
1// Cursor coordinate functions
2var myX, myY, xyOn, myMouseX, myMouseY;
3xyOn = true;
4
5function getXYPosition(e) {
6 myMouseX = (e || event).clientX;
7 myMouseY = (e || event).clientY;
8 if (document.documentElement.scrollTop > 0) {
9 myMouseY = myMouseY + document.documentElement.scrollTop;
10 }
11 if (xyOn) {
12 alert("X is " + myMouseX + "\nY is " + myMouseY);
13 }
14}
15function toggleXY() {
16 xyOn = !xyOn;
17 document.getElementById('xyLink').blur();
18 return false;
19}
20
21document.onmouseup = getXYPosition;
1<p>Click in the div element below to get the x (horizontal) and y (vertical) coordinates of the mouse pointer, when it is clicked.</p>
2
3<div onclick="showCoords(event)"><p id="demo"></p></div>
4
5<p><strong>Tip:</strong> Try to click different places in the div.</p>
6
7<script>
8function showCoords(event) {
9 var cX = event.clientX;
10 var sX = event.screenX;
11 var cY = event.clientY;
12 var sY = event.screenY;
13 var coords1 = "client - X: " + cX + ", Y coords: " + cY;
14 var coords2 = "screen - X: " + sX + ", Y coords: " + sY;
15 document.getElementById("demo").innerHTML = coords1 + "<br>" + coords2;
16}
17</script>