Simulate Mouse Event

V

vunet.us

Hello,
My function gets mouse coordinates like this:
function getMouseCoords(e){
e = (e) ? e : window.event;
var x = 0;
var y = 0;
if(e.pageX){
x = e.pageX;
y = e.pageY;
}else if(e.clientX){
x = e.clientX + document.body.scrollLeft - document.body.clientLeft;
y = e.clientY + document.body.scrollTop - document.body.clientTop;
}
return new Array(x, y);
}

To run the function I pass event to it:
obj.onclick = runMe;
function runMe(ev){
ev = ev || window.event;
var mousecoords = getMouseCoords(ev);
}

Question: how to simulate or avoid using "ev" variable in runMe() but
get mouse coordinates anyway? I simply want to run runMe() in many
places and subroutines, so passing event seems to be a complicated
task.
Thanks
 
T

Thomas 'PointedEars' Lahn

[...]
To run the function I pass event to it:
obj.onclick = runMe;
function runMe(ev){
ev = ev || window.event;
var mousecoords = getMouseCoords(ev);
}

Question: how to simulate or avoid using "ev" variable in runMe() but
get mouse coordinates anyway?

Not possible. An event listener has a built-in fixed signature.
I simply want to run runMe() in many places and subroutines, so
passing event seems to be a complicated task.

Even with event capturing, if you pass `event' from an event handler
attribute to runMe(), the lines

e = (e) ? e : window.event;

in getMouseCoords() and

ev = ev || window.event;

in runMe() are unnecessary.

However, you are looking for event bubbling, where you would need only one
event listener:

<head>
<!-- ... -->
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function handleClick(e)
{
if (e)
{
var mousecoords = getMouseCoords(e);
}
}
</script>
</head>

<body onclick="handleClick(e)">
<!-- ... -->
</body>

See http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling


HTH

PointedEars
 
V

vunet.us

[...]
To run the function I pass event to it:
obj.onclick = runMe;
function runMe(ev){
ev = ev || window.event;
var mousecoords = getMouseCoords(ev);
}
Question: how to simulate or avoid using "ev" variable in runMe() but
get mouse coordinates anyway?

Not possible. An event listener has a built-in fixed signature.
I simply want to run runMe() in many places and subroutines, so
passing event seems to be a complicated task.

Even with event capturing, if you pass `event' from an event handler
attribute to runMe(), the lines

e = (e) ? e : window.event;

in getMouseCoords() and

ev = ev || window.event;

in runMe() are unnecessary.

However, you are looking for event bubbling, where you would need only one
event listener:

<head>
<!-- ... -->
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function handleClick(e)
{
if (e)
{
var mousecoords = getMouseCoords(e);
}
}
</script>
</head>

<body onclick="handleClick(e)">
<!-- ... -->
</body>

Seehttp://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-bubbling

HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <[email protected]>

but "e" in <body onclick="handleClick(e)"> is not defined, is it?!
 
T

Thomas 'PointedEars' Lahn

is addEventListener something I may be considering?

No, it would require full support for DOM Level 2 Events, and be overkill
anyway (unless you were not dealing with [X]HTML). IE still doesn't have
that, for example.


PointedEars
 
D

David Mark

is addEventListener something I may be considering?

If you need multiple listeners for the click event. You would need to
do some feature detection as IE does not support addEventListener (it
uses attachEvent.)

Also, realize that your solution will fail in IE unless your document
triggers quirks mode.

You need to check for the layout mode (document.compatMode), which
determines whether document.body or document.documentElement will
provide the scroll and client offsets.

Furthermore, it will fail in other browsers if pageX is 0. You need
to check if pageX is a number.
 

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

Members online

No members online now.

Forum statistics

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

Latest Threads

Top