mouse cursor position in Javascript?

19,958

I assume you don't want a framework answer. If not, try this:

document.onclick=function(evt) {
    evt = (evt || event);
    alert(evt.clientX + ' ' + evt.clientY);
}

I wasn't sure what event you wanted, so I just used an onclick event.

Share:
19,958

Related videos on Youtube

Roger Travis
Author by

Roger Travis

Updated on April 27, 2022

Comments

  • Roger Travis
    Roger Travis about 2 years

    How do I get cursor X and Y in javascript?

    var $curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
    var $curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
    

    I had found this two, but they appear as "undefined". Any ideas?

    Thanks!

    • Pointy
      Pointy almost 14 years
      It's a good rule-of-thumb that any page with advice on how to deal with Netscape 6 is probably pretty old and therefore suspect. (Actually, if it's not old it's probably even more suspect!)