How to find the coordinates of the cursor relative to the screen with JavaScript?

HTMLJavascriptProgramming Scripts

To find the coordinates of the cursor in JavaScript, use event.screenX and event.screenY.

Example

You can try to run the following code to get cursor coordinates relative to the screen in JavaScript.

<!DOCTYPE html>
<html>
   <head>
      <script>
         function coordinatesFunc(event) {
            document.write("Coordinate(X) = " + event.screenX + "<br>Coordinate(Y) = " + event.screenY);
         }
      </script>
   </head>
   <body>
      <div onmousedown = "coordinatesFunc(event)">Click me to get coordinates of cursor relative to screen.</p>
   </body>
</html>
raja
Published on 26-Feb-2018 17:38:21
Advertisements