March 24th, 2006

JavaScript – Get Mouse Position

This little function will retrieve the X & Y (horizontal & vertical) position of the mouse cursor in Internet Explorer, FireFox, and all other browsers.


function moveObject( e ) {
    var tempX = 0;
    var tempY = 0;
    if (document.all) {
        // must be IE
        tempX = event.clientX + document.body.scrollLeft;
        tempY = event.clientY + document.body.scrollTop;
    } else {
        tempX = e.pageX;
        tempY = e.pageY;
    }
    // prevent any negative values
    if (tempX < 0){tempX = 0}
    if (tempY < 0){tempY = 0}
    alert("X:" + tempX + "\r\nY:" + tempY);
}
  • Share/Bookmark

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word