Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Simulate Mouse Event

Reply
Thread Tools

Simulate Mouse Event

 
 
vunet.us@gmail.com
Guest
Posts: n/a
 
      08-08-2007
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

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-08-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> [...]
> 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-Eve...-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, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
vunet.us@gmail.com
Guest
Posts: n/a
 
      08-08-2007
On Aug 8, 5:28 pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> (E-Mail Removed) wrote:
> > [...]
> > 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, <f806at$ail$1$(E-Mail Removed)>


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

 
Reply With Quote
 
vunet.us@gmail.com
Guest
Posts: n/a
 
      08-08-2007
is addEventListener something I may be considering?

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-08-2007
(E-Mail Removed) wrote:
>> [...]

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


It isn't. Replace that `e' with `event'. WFM in MSHTML, Gecko, Opera.

And please trim your quotes.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-08-2007
(E-Mail Removed) wrote:
> 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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      08-09-2007
On Aug 8, 6:04 pm, (E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
vunet.us@gmail.com
Guest
Posts: n/a
 
      08-09-2007

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

>
> It isn't. Replace that `e' with `event'. WFM in MSHTML, Gecko, Opera.
>
> And please trim your quotes.
>

thanks! works!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to simulate Mouse Click with Ruby, or mouse library Kid Kid Ruby 6 05-08-2007 12:43 PM
determine the mouse cursor position without mouse event tom arnall Java 6 01-18-2007 07:27 PM
How to simulate the mouse control? flplini@i-image.com.tw C++ 4 10-24-2006 01:41 AM
Heres a mouse theres a mouse what a mouse do? unholy Gaming 37 09-17-2006 08:59 AM
Simulate Mouse/Keyboard to stop Screen Saver Michael J Whitmore Python 1 11-06-2003 06:47 PM



Advertisments