mouse coordinates undefined

F

finerrecliner

trying to get the mouse coordinates to show up in an alert box. but all
i get is "undefined"
the script should work like:
click the button, then click anywhere on the page to show your current
x coord, then click again anywhere on the page to show current Y coord,
then go back to normal mouse behavior.

must work in firefox 1.5+
Thanks!


<html>
<head>
<script type="text/javascript">
var x = 0;
function click()
{
if(x == 0)
{
//stops anything from happening when you click the
button
x = 1;
}
else if(x == 1)
{
mouseX(document.onclick);
x = 2;
}
else if(x == 2)
{
mouseY(document.onclick);
x = 0;
document.onclick=null;
}
else { alert("wtf");}
}
function getcoords()
{
document.onclick=click;
}
function mouseX(evt)
{
var Xcoord = evt.pageX;
alert(Xcoord);
}
function mouseY(evt)
{
var Ycoord = evt.pageY;
alert(Ycoord);
}
</script>
</head>
<body>
<button onclick="getcoords();">click here</button>
</body>
</html>
 
U

ulrich.kautz

function click()
{
if(x == 0)
{
//stops anything from happening when you click the
button
x = 1;
}
else if(x == 1)
{
mouseX(document.onclick);
x = 2;
}
else if(x == 2)
{
mouseY(document.onclick);
x = 0;
document.onclick=null;
}
else { alert("wtf");}
}


your "click"-function will get as (first) argument a
window-event-object. just forward this to your "mouseX" and
"mouseY"-function:
function click() {
....
mouseX(arguments[0]);
...
mouseY(arguments[0]);
....
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top