Friday, July 6, 2007

Get Mouse Position Using JavaScript

IE Supported

<html>
<body>
<script language="JavaScript">
<!--
document.onmousemove = getCoordinate;
var mosX = 0 ;
var mosY = 0 ;
function getCoordinate(e)
{
mosX = event.clientX + document.body.scrollLeft ;


//clientX Property Sets or retrieves the x-coordinate of the mouse
//pointer's position relative to the client area of the window,
//excluding window decorations and scroll bars
//scrollLeft Property Sets or retrieves the distance between the
//left edge of the object and the leftmost portion of the content
//currently visible in the window.


mosY = event.clientY + document.body.scrollTop;

//clientY Property Sets or retrieves the y-coordinate of the mouse
//pointer's position relative to the client area of the window,
//excluding window decorations and scroll bars.
//scrollTop Property Sets or retrieves the distance between the top
//of the object and the topmost portion of the content currently
//visible in the window.


document.title = "(X Co-Ordinate » "+ mosX +") ( "+"Y Co-ordinate » " +mosY+")";
document.getElementById('dx').innerHTML = "Mouse X ==» "+mosX+"<br>"+"Mouse Y ==» "+mosY;


return true
}
//-->
</script>
<div id="dX"></div>
</body>
</html>



1 comment:

she7ata said...

This is good, thank you sir, i was wondering why microsoft changed the window.screenX and screenY, this is took me time to understand, you will need to have another code to handle the same function for FrieFox or Chrome, they still support screenX and screenY

regards,,

Creative Commons License
Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.