How to get the position of the mouse pointer when the user clicks on the screen?
Through this article you can find the coordinate of the screen using JavaScript. We put the event handler OnMouseDown in BODY section of the HTML page like this:
<body onMouseDown = XYpos()>
Now, every time the mouse is clicked anywhere in the Body of the web page, the function XYpos() gets called.
X coordinate tells horizontal position of the screen and Y coordinate tells the vertical position of the screen.
Now write the following script in the HEAD section of the html page.
<script type="text/javascript">
function XYpos()
{
xPos = event.screenX
yPos = event.screenY
alert(xPos + " left " + yPos + " down")
}
</script>
Here xPos and yPos will hold the horizontal and vertical position of the screen where mouse clicked. Both coordinates will display in the alert box.
Example:
<html>
<head>
<title>Coordinate on the screen</title>
<script type="text/javascript">
function XYpos()
{
xPos = event.screenX
yPos = event.screenY
alert(xPos + " left " + yPos + " down")
}
</script>
</head>
<body onMouseDown = XYpos() background="bk15a.gif">
<font color=navy >Click anywhere on the screen to see the X and Y coordinates
</font>
</body>
</html>
Output: Output of the above script is something like this:
Figure 1: Output the script.
When you will click anywhere on the screen then alert box return the X and Y coordinates of that point (see the figure 2).
Figure 2: Alert box return the coordinates.