1
var mouse = function(e){
    var x = e.pageX || (e.clientX + document.body.scrollLeft);
    var y = e.pageY || (e.clientY + document.body.scrollTop);

    alert(x + " " + y);
};
window.onload = function(e) {
    if(!e) { var e = window.event; }
    mouse(e);
};

This works in IE and I get the proper mouse x and y coordinates. However, when ran in something other then IE I just get an alert "NaN NaN"

(I've tried searching)

2
  • 2
    This looks wrong in many ways. I think you want onmousemove and not onload
    – mplungjan
    Commented Jul 7, 2011 at 5:39
  • If you are using FireFox 4 (or 5), then the Web Console can help you debug this. Otherwise, i'd pull up firebug and debug the function Commented Jul 7, 2011 at 5:40

2 Answers 2

3

To me this looks wrong in many ways. I think you want onmousemove and not onload - possibly document.onmousedown - and you do not want to alert. Instead change some innerHTML or document.title

Do you want the click or the move?

document.onmousemove = function(e){
    var x = e.pageX || (e.clientX + document.body.scrollLeft);
    var y = e.pageY || (e.clientY + document.body.scrollTop);
    document.title=x + " " + y;
};

Also have a look here

Responding to the onmousemove event outside of the browser window in IE

0
1

This is because load events in most browsers don't have clientX and clientY properties.

Change your onload function to the following:

window.onload = function (e) {
    if (!e) { 
        var e = window.event; 
    }
    alert("type is " + e.type + " clientX is " + e.clientX);
    mouse(e);
};

and you'll see, for non-IE browsers, that the type is a load event, but clientX is undefined.

Not the answer you're looking for? Browse other questions tagged or ask your own question.