views:

32

answers:

2

I want to get mouse x and y co-ordinates through JavaScript. I have tried pageX, pageY, clientX, clientY, but nothing is displayed. I have tested this on both IE and Firefox.

Is there something I'm doing wrong in the following event?

var x=0;
var y=0;
function mouseDown(){
    alert("This is displayed");
    evt=event||window.event;
    alert(evt.clientX);
        alert(evt.clientY);
            alert(evt.pageX);
                alert(evt.pagey);
    alert("Flow doesn't reach here...");            
}
+1  A: 

Yes - you also need to hook the function up as an event handler.

Your event should take an event parameter:

function mouseDown(event) {

And then you need to tell the browser to call your function when the event occurs:

document.onmousedown = mouseDown;
sje397
A: 

Use JQuery, here is a good example how to implement it.

rdkleine
no i dn't wnt 2 use jquery....i can do it in jquery i wnt to do it with javascript....
suraj
srsly? mst b md
rdkleine
iz fanny youz tink jq iz nut java.
zaf
@zaf cmply agr!
rdkleine