SearchSearch   

Simulate Mouse Event

 
   Webmaster Forums (Home) -> Javascript RSS
Next:  Dynamic Script Insertion Revisited  
Author Message
vunet.us

External


Since: Jun 04, 2007
Posts: 65



(Msg. 1) Posted: Wed Aug 08, 2007 6:58 pm
Post subject: Simulate Mouse Event
Archived from groups: comp>lang>javascript (more info?)

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
Back to top
David Mark

External


Since: Aug 06, 2007
Posts: 358



(Msg. 2) Posted: Wed Aug 08, 2007 7:30 pm
Post subject: Re: Simulate Mouse Event
Archived from groups: per prev. post (more info?)

On Aug 8, 6:04 pm, vunet... DeleteThis @gmail.com 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.
Back to top
vunet.us

External


Since: Jun 04, 2007
Posts: 65



(Msg. 3) Posted: Wed Aug 08, 2007 9:54 pm
Post subject: Re: Simulate Mouse Event
Archived from groups: per prev. post (more info?)

On Aug 8, 5:28 pm, Thomas 'PointedEars' Lahn <PointedE... DeleteThis @web.de>
wrote:
> vunet... DeleteThis @gmail.com 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$8300d...@news.demon.co.uk>

but "e" in <body onclick="handleClick(e)"> is not defined, is it?!
Back to top
vunet.us

External


Since: Jun 04, 2007
Posts: 65



(Msg. 4) Posted: Wed Aug 08, 2007 10:04 pm
Post subject: Re: Simulate Mouse Event
Archived from groups: per prev. post (more info?)

is addEventListener something I may be considering?
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 5) Posted: Wed Aug 08, 2007 11:28 pm
Post subject: Re: Simulate Mouse Event
Archived from groups: per prev. post (more info?)

vunet.us.DeleteThis@gmail.com 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-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$8300dec7@news.demon.co.uk>
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 6) Posted: Thu Aug 09, 2007 12:20 am
Post subject: Re: Simulate Mouse Event
Archived from groups: per prev. post (more info?)

vunet.us RemoveThis @gmail.com 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
Back to top
Thomas 'PointedEars' Lahn

External


Since: Sep 05, 2004
Posts: 3405



(Msg. 7) Posted: Thu Aug 09, 2007 12:22 am
Post subject: Re: Simulate Mouse Event
Archived from groups: per prev. post (more info?)

vunet.us.DeleteThis@gmail.com 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
Back to top
vunet.us

External


Since: Jun 04, 2007
Posts: 65



(Msg. 8) Posted: Thu Aug 09, 2007 6:35 am
Post subject: Re: Simulate Mouse Event
Archived from groups: per prev. post (more info?)

>
> > 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!
Back to top
Display posts from previous:   
       Webmaster Forums (Home) -> Javascript
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum