Connecting Tech Pros Worldwide Help | Site Map

srcElement in IE = what in Netscape?

Dan
Guest
 
Posts: n/a
#1: Jul 20 '05
Hi,

When i click down with the mouse, i want to be sure that the image "myimg"
is clicked before doing something. With IE i use 'srcElement' and it works:

<IMG ID="myimg" SRC="bugs.gif" onMouseDown="downtest()">
<script>
function downtestIE()
{strid =window.event.srcElement.id
if (strid=="myimg")
{ etc ...

But how to do the same for Netscape? I tried with 'currentTarget' but get
always: 'e has no properties'
<script>
var e;
function downtest(e)
{strid=e.currentTarget
if (strid=="myimg")
{ etc ...

Any help is welcome
Dan


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

re: srcElement in IE = what in Netscape?


"Dan" <hgfghfg@dfgd.rf> writes:
[color=blue]
> When i click down with the mouse, i want to be sure that the image "myimg"
> is clicked before doing something. With IE i use 'srcElement' and it works:[/color]

The official name is "target". Also, there is no "window.event" in
Mozilla/Netscape, that is a Microsoft invention as well.
[color=blue]
> <IMG ID="myimg" SRC="bugs.gif" onMouseDown="downtest()">[/color]
[color=blue]
> <script>[/color]

<script type="text/javascript">

The type attribute is mandatory.
[color=blue]
> function downtestIE()[/color]
Try:

function downtest(event) {
event = event || window.event; // IE doesn't pass event as argument.
var tgt = event.target || event.srcElement; // IE doesn't use .target
var strid = tgt.id;
[color=blue]
> if (strid=="myimg")
> { etc ...[/color]

/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.'
Dan
Guest
 
Posts: n/a
#3: Jul 20 '05

re: srcElement in IE = what in Netscape?


Thanks for replying...
I tried different ways about this and maybe you can help me understand some
differences:
1) this works ('ev' is used instead of 'event')
<IMG ID="myimg" SRC="bugs.gif" >
<script type="text/javascript">
var ev;
lap=document.getElementById("myimg")
function downtest(ev)
{
var strid = ev.target.id;
alert(strid)
}
lap.onmousedown=downtest // WHY NOT
lap.onmousedown=downtest(ev) ??
</script>

2) this works too ('event' used)
<IMG ID="myimg" SRC="bugs.gif" onMouseDown=downtest(event)>
<script type="text/javascript">
lap=document.getElementById("myimg")
function downtest(event)
{
var strid = event.target.id;
alert(strid)
}
</script>

3) this doesn't work: why can 'event' not be replaces by 'ev'? error= ev has
no properties
<IMG ID="myimg" SRC="bugs.gif" onMouseDown=downtest(ev)>
<script type="text/javascript">
var ev;
lap=document.getElementById("myimg")
function downtest(ev)
{
var strid = ev.target.id;
alert(strid)


thanks for your time
Dan

"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:ptjusq56.fsf@hotpop.com...[color=blue]
> "Dan" <hgfghfg@dfgd.rf> writes:
>[color=green]
> > When i click down with the mouse, i want to be sure that the image[/color][/color]
"myimg"[color=blue][color=green]
> > is clicked before doing something. With IE i use 'srcElement' and it[/color][/color]
works:[color=blue]
>
> The official name is "target". Also, there is no "window.event" in
> Mozilla/Netscape, that is a Microsoft invention as well.
>[color=green]
> > <IMG ID="myimg" SRC="bugs.gif" onMouseDown="downtest()">[/color]
>[color=green]
> > <script>[/color]
>
> <script type="text/javascript">
>
> The type attribute is mandatory.
>[color=green]
> > function downtestIE()[/color]
> Try:
>
> function downtest(event) {
> event = event || window.event; // IE doesn't pass event as argument.
> var tgt = event.target || event.srcElement; // IE doesn't use .target
> var strid = tgt.id;
>[color=green]
> > if (strid=="myimg")
> > { etc ...[/color]
>
> /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.'[/color]


Dan
Guest
 
Posts: n/a
#4: Jul 20 '05

re: srcElement in IE = what in Netscape?


thanks


"Lasse Reichstein Nielsen" <lrn@hotpop.com> schreef in bericht
news:lluiibzy.fsf@hotpop.com...[color=blue]
> "Dan" <no@mail.xy> writes:
>
> Please don't top-post.
>[color=green]
> > I tried different ways about this and maybe you can help me understand[/color][/color]
some[color=blue][color=green]
> > differences:[/color]
>[color=green]
> > 1) this works ('ev' is used instead of 'event')
> > <IMG ID="myimg" SRC="bugs.gif" >
> > <script type="text/javascript">
> > var ev;[/color]
>
> This (global) variable declaration is not necessary. The (local) function
> parameter overshadows it inside the function, and you never assign[/color]
anything[color=blue]
> to the global variable.
>[color=green]
> > lap=document.getElementById("myimg")
> > function downtest(ev)
> > {
> > var strid = ev.target.id;
> > alert(strid)
> > }
> > lap.onmousedown=downtest // WHY NOT[/color]
>
> Why not what? It looks absolutely correct, although I would end my
> sentences with a semicolon. Mostly because I find it more readable.
>[color=green]
> > lap.onmousedown=downtest(ev) ??[/color]
>
> This is not what you want. You don't want to call the function now
> and assigne the result (which is "undefined" since there is no return
> statment in it) to "lap.onmousedown".
>
> If you have both of these lines, the latter overwrites the former,
> which might be why you don't see the result.
>[color=green]
> > </script>
> >
> > 2) this works too ('event' used)
> > <IMG ID="myimg" SRC="bugs.gif" onMouseDown=downtest(event)>[/color]
>
> You should have quotes around the "onmousedown" attribute value,
> since it contains "(" and ")". In practice, it is easier to always
> put quotes around than to worry which charaters are legal and
> which aren't.
>
> You call the "downtest" function with the value of the "event"
> variable. The javascript code in the onmousedown attribute value is
> evaluated in a context, where "event" refers to the current event
> (probably why Microsoft decided to just have one global event
> variable). The *value* of the variable "event" is used as argument
> to the downtest function.
>[color=green]
> > <script type="text/javascript">
> > lap=document.getElementById("myimg")
> > function downtest(event)[/color]
>
> The name of the function argument is irrelevant, you can call it "ev",
> "event", "foo" or even "body" without affecting how this function works.
> A function argument is local to the function, so
> ---
> var x=4;
> function foo(x){
> x=2;
> return x;
> }
> foo(12);
> alert(x);
> ---
> will alert "4". The variable "x" outside the function and the one inside
> are two different variables, and the following is *completely* equivalent:
> ---
> var x=4;
> function foo(y){
> y=2;
> return y;
> }
> foo(12);
> alert(x);
> ---
> (in programming language theory, that is a well known concept: renaming
> of "bound" variables doesn't change the behavior of the program).
>[color=green]
> > {
> > var strid = event.target.id;
> > alert(strid)
> > }
> > </script>[/color]
>
>[color=green]
> > 3) this doesn't work: why can 'event' not be replaces by 'ev'? error= ev[/color][/color]
has[color=blue][color=green]
> > no properties
> > <IMG ID="myimg" SRC="bugs.gif" onMouseDown=downtest(ev)>[/color]
>
> Here the code "downtest(ev)" is executed in an environment where
> there is a variable, called "event", referring to the current event.
> The "ev" variable is the one you declare below, and it only contains
> "undefined".
>[color=green]
> > <script type="text/javascript">
> > var ev;
> > lap=document.getElementById("myimg")
> > function downtest(ev)
> > {
> > var strid = ev.target.id;[/color]
>
> That means that at this point, the local variable (also called "ev")
> has the value "undefined", and you can't find the property "target"
> of a value that is "undefined".
>
> /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.'[/color]


Closed Thread


Similar JavaScript / Ajax / DHTML bytes