Introduction:
In this article we will discuss about how to detect the position of the Mouse Cursor using javascript.
Main:
Just add this below code in yours browser,
function FindCursorPosition(event)
{
if (typeof event == “undefined”)
{
event = window.event;
}
var scrollingPosition = getScrollingPosition();
var cursorPosition = [0, 0];
if (typeof event.pageX != “undefined” &&
typeof event.x != “undefined”)
{
cursorPosition[0] = event.pageX;
cursorPosition[1] = event.pageY;
}
else
{
cursorPosition[0] = event.clientX + scrollingPosition[0];
cursorPosition[1] = event.clientY + scrollingPosition[1];
}
var paragraph = document.getElementsByTagName(“p”)[0];
paragraph.replaceChild(document.createTextNode(
“Your mouse is currently located at: ” + cursorPosition[0] +
“,” + cursorPosition[1]), paragraph.firstChild);
return true;
}
<input type=button id=findbutton onclick=”FindCursorPosition(this.Click);”>
Conclusion:
Hope,this helps,
Happy Coding.