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>
1(function() {
2 document.onmousemove = handleMouseMove;
3 function handleMouseMove(event) {
4 var eventDoc, doc, body;
5
6 event = event || window.event; // IE-ism
7
8 // If pageX/Y aren't available and clientX/Y are,
9 // calculate pageX/Y - logic taken from jQuery.
10 // (This is to support old IE)
11 if (event.pageX == null && event.clientX != null) {
12 eventDoc = (event.target && event.target.ownerDocument) || document;
13 doc = eventDoc.documentElement;
14 body = eventDoc.body;
15
16 event.pageX = event.clientX +
17 (doc && doc.scrollLeft || body && body.scrollLeft || 0) -
18 (doc && doc.clientLeft || body && body.clientLeft || 0);
19 event.pageY = event.clientY +
20 (doc && doc.scrollTop || body && body.scrollTop || 0) -
21 (doc && doc.clientTop || body && body.clientTop || 0 );
22 }
23
24 // Use event.pageX / event.pageY here
25 }
26})();
27