find mouse position and coordinates

How to find mouse position and coordinates

Many times we worked on this topic. In this tutorial we are going to show you, How to find mouse position and coordinates. we given code sample in article.

How to find mouse position and coordinates

[script]
/*
* This get_mouse_coordinates function we are using for capturing the Mouse coordinates of on browser
*/
var isIE = document.all?true:false;
if (!isIE) document.captureEvents(Event.MOUSEMOVE);
document.load = get_mouse_coordinates;
document.onmousemove = get_mouse_coordinates;

function get_mouse_coordinates(e) { // as per Document body

var x_pos;
var y_pos;
if (!isIE) {
x_pos = e.pageX;
y_pos = e.pageY;
}
if (isIE) {
x_pos = event.clientX + document.body.scrollLeft;
y_pos = event.clientY + document.body.scrollTop;
}

alert(x_pos);
alert(y_pos);
}

[/script]

clientX and clientY will work in only Firefox, safari, chrome and opeara. That will not work in IE browsers.

So We need to write condition for IE browsers.
In IE browser use window.event.x and window.event.y methods.

Given example is cross browser tested…

 find mouse position and coordinates
find mouse position and coordinates

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.