Connecting Tech Pros Worldwide Forums | Help | Site Map

How to call event as a function in mozilla

Jadow
Guest
 
Posts: n/a
#1: Jul 20 '05
I have a function that is used as an event for some parts of the
script.
i.e.
function somethind(e) {
if (!e.currentTarget.documentElement ){
...
}
}

I need to call this function manually (not by assigning it to an
event) Yet I cannot seem to deal with 'e'.
If I call it as: something(e); I get a 'not defined', if I ommit it I
get an undefined.
For some reason 'e' is undefined in the calling script. What decides
when it is available as an event object? and how can I resolve this
issue?

Thanks
Joshua

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 20 '05

re: How to call event as a function in mozilla


cyberwombat@yifan.net (Jadow) writes:
[color=blue]
> I have a function that is used as an event for some parts of the
> script.[/color]

It is an event handler. The "e" will be the event then.
[color=blue]
> function somethind(e) {
> if (!e.currentTarget.documentElement ){
> ...
> }
> }[/color]

That requires the currentTarget property to be a document object.
[color=blue]
> I need to call this function manually (not by assigning it to an
> event) Yet I cannot seem to deal with 'e'.
> If I call it as: something(e); I get a 'not defined', if I ommit it I
> get an undefined.[/color]

If you write
something(e);
and "e" is not a defined variable, then it is ofcourse an error. You have
to add an event as argument (or any object with a currentTarget property).
Try
something({currentTarget:document});
[color=blue]
> For some reason 'e' is undefined in the calling script.[/color]

That reason is proabably that it hasn't been defined.
[color=blue]
> What decides when it is available as an event object?[/color]

When you assign it. The "e" in the function definition only
exists inside the function. It is a local name for the argument
to the function.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Newbie
 
Join Date: Jul 2006
Posts: 2
#3: Jul 3 '06

re: How to call event as a function in mozilla


There's an article here entitled "Firefox: 'event is not defined'" that explains exactly why this happens and how to fix it (so it works in both IE and Mozilla):

http://www.theblueform.com/Home/TheMakingOf.aspx

Well worth a read to understand why it happens. Lots of other good JavaScript advice here too.
Newbie
 
Join Date: Jul 2006
Posts: 3
#4: Jul 15 '06

re: How to call event as a function in mozilla


Hi,

I was using the command: event.srcElement.value = ""; in a js function which function perfectly in IE. The function was called on the onchange event of a textbox.

I read the article you'd mentioned, but didn't quite understand how to apply it in this case. Can you help me out? Let's say this is the code:

...
<input type="text" id="abc" onchange="return test();">
...

<script language="javascript">
function test()
{
event.srcElement.value = "";
}
</script>
(Works in IE, gives an error (event not defined) in Firefox)
Closed Thread