views:

77

answers:

2

Hi All,

Just wondering if its possible to get the x/y location of the mouse from the onload event of the document (before any mousemove events).

Thanks

Guido

+1  A: 

short answer: no

long answer: yes. the onload event doesn't provide info on the mouse position, however you can set a variable when onload has fired, and use the onmousemove event on the document to get the mouseposition as soon as the mouse moves after the documents loads (after the variable has been set). Not what you wanted, though.

Jonathan Fingland
Didn't think it was possible, but I thought perhaps someone knew a hack. Thanks for verifying.
gatapia
A: 

You can try something like:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript">
function SetValues() {
  var IE = document.all?true:false;
  if (!IE) document.captureEvents(Event.MOUSEMOVE)
  getMouseXY();
  var mX = 0;
  var mY = 0;
  function getMouseXY(e) {
    if (IE) {
      mX = event.clientX + document.body.scrollLeft;
      mY = event.clientY + document.body.scrollTop;
    }
    else {
      mX = e.pageX;
      mY = e.pageY;
    }  

    var s = 'X=' + mX +  ' Y=' + mY ;
    document.getElementById('divCoord').innerHTML = s;
    return true;
  }
}
</script></head>
<body onload=SetValues()>
<div id="divCoord"></div>
</body></html>
Jonathan