Posted on 2006-05-29
Last Modified: 2006-11-18
  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?
Question by:RockyFullen
    LVL 17

    Accepted 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.
    LVL 17

    Expert Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now