Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:


  I am building a web page in which I need to know the specific coordinates of the mouse at all times.
I have plugged in a script I have found, which seems to capture the coordinates ok. THe problem is that
there is sometimes a delay, and other times I move the mouse, the browser simply does not respond to it.
I have implemented document.onmousemove, and assigned to a function. Is there a way to improve overall
response time, and remove any delay of the detected movement?
  • 2
1 Solution
Use attachEvent or EventListener or combination of both to capture the mouse events and then capture the coordinates.
You have to use both only if you need to bring browser compatibility.
Try this code:

            <script language="JavaScript1.2">
                  var IE = document.all?true:false;
                  if (!IE) document.captureEvents(Event.MOUSEDOWN)
                  document.onmousedown = getMouseXY;
                  var tempX = 0;
                  var tempY = 0;
                  function getMouseXY(e) {
                        if (IE) { // grab the x-y pos.s if browser is IE
                              tempX = event.clientX + document.body.scrollLeft;
                              tempY = event.clientY + document.body.scrollTop;
                        }else {  // grab the x-y pos.s if browser is NS
                              tempX = e.pageX;
                              tempY = e.pageY;
                        if (tempX < 0){tempX = 0;}
                        if (tempY < 0){tempY = 0;}  
                        document.Show.MouseX.value = tempX;
                        document.Show.MouseY.value = tempY;
                        return true;

<form name="Show">
X <input type="text" name="MouseX" value="0" size="4"><br>
Y <input type="text" name="MouseY" value="0" size="4"><br>


Hope this is what you wanted

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now