F
finerrecliner
what i'm trying to accomplish:
user clicks button. then can click 2 more times anywhere on the page
and display his mouse coordinates. after those 2 clicks, go back to
normal mouse behavior
the code below does exactly that, but i dont understand how my click
function got the event argument, since i never passed anything to
it(see denoted line)? why does this work?
note: if i use the line document.onclick=click(e);
it won't work correctly, and will only call the click function when the
user clicks the button (rather than anywhere on the page).
<html>
<head>
<script type="text/javascript">
var x = 0;
function getcoords(e)
{
if (!e) var e = window.event;
document.onclick=click; //why does this line work?!
}
function click(e)
{
if(x == 0)
{
x = 1;
}
else if(x == 1)
{
alert(e.clientX + " , " + e.clientY);
x = 2;
}
else if(x == 2)
{
alert(e.clientX + " , " + e.clientY);
x = 0;
document.onclick=null;
}
else { alert("wtf");}
}
</script>
</head>
<body>
<button onclick="getcoords(event);">click here</button>
</body>
</html>
user clicks button. then can click 2 more times anywhere on the page
and display his mouse coordinates. after those 2 clicks, go back to
normal mouse behavior
the code below does exactly that, but i dont understand how my click
function got the event argument, since i never passed anything to
it(see denoted line)? why does this work?
note: if i use the line document.onclick=click(e);
it won't work correctly, and will only call the click function when the
user clicks the button (rather than anywhere on the page).
<html>
<head>
<script type="text/javascript">
var x = 0;
function getcoords(e)
{
if (!e) var e = window.event;
document.onclick=click; //why does this line work?!
}
function click(e)
{
if(x == 0)
{
x = 1;
}
else if(x == 1)
{
alert(e.clientX + " , " + e.clientY);
x = 2;
}
else if(x == 2)
{
alert(e.clientX + " , " + e.clientY);
x = 0;
document.onclick=null;
}
else { alert("wtf");}
}
</script>
</head>
<body>
<button onclick="getcoords(event);">click here</button>
</body>
</html>