473,246 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

srcElement in IE = what in Netscape?

Dan
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
Jul 20 '05 #1
3 12658
"Dan" <hg*****@dfgd.rf> writes:
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:
The official name is "target". Also, there is no "window.event" in
Mozilla/Netscape, that is a Microsoft invention as well.
<IMG ID="myimg" SRC="bugs.gif" onMouseDown="downtest()"> <script>
<script type="text/javascript">

The type attribute is mandatory.
function downtestIE() 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;
if (strid=="myimg")
{ etc ...


/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Dan
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" <lr*@hotpop.com> wrote in message
news:pt**********@hotpop.com...
"Dan" <hg*****@dfgd.rf> writes:
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:
The official name is "target". Also, there is no "window.event" in
Mozilla/Netscape, that is a Microsoft invention as well.
<IMG ID="myimg" SRC="bugs.gif" onMouseDown="downtest()">

<script>


<script type="text/javascript">

The type attribute is mandatory.
function downtestIE()

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;
if (strid=="myimg")
{ etc ...


/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #3
Dan
thanks
"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:ll**********@hotpop.com...
"Dan" <no@mail.xy> writes:

Please don't top-post.
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;


This (global) variable declaration is not necessary. The (local) function
parameter overshadows it inside the function, and you never assign

anything to the global variable.
lap=document.getElementById("myimg")
function downtest(ev)
{
var strid = ev.target.id;
alert(strid)
}
lap.onmousedown=downtest // WHY NOT


Why not what? It looks absolutely correct, although I would end my
sentences with a semicolon. Mostly because I find it more readable.
lap.onmousedown=downtest(ev) ??


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.
</script>

2) this works too ('event' used)
<IMG ID="myimg" SRC="bugs.gif" onMouseDown=downtest(event)>


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.
<script type="text/javascript">
lap=document.getElementById("myimg")
function downtest(event)


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).
{
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)>


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".
<script type="text/javascript">
var ev;
lap=document.getElementById("myimg")
function downtest(ev)
{
var strid = ev.target.id;


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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Thies | last post by:
I have this IE specific bit of code for finding the originating node: var obj=window.event.srcElement; How do I do that cross browser (Opera, NS, Safari...)? Is there a standard DOM method? ...
4
by: Josselin | last post by:
I am loading a group of items (urls of images to load) group.items for each items I associate the filename and an <img> object on my page using : .... var pair = new ImagePair(group.items);...
9
by: stevewy | last post by:
I am trying to write a function that will test all the checkboxes in a particular group of a form (it's a questionnaire), see whether more than three of them are ticked, and display a message if...
4
by: stevewy | last post by:
If I am using srcElement (or "target" for non-IE models) to return various properties of an object I have clicked on, can I access for "label for" value in any way? I'm thinking, for example, of...
3
by: Jake G | last post by:
Ok. I have figured out my whole script except how to make it work in FF. It is a script that lets a user know how many characters they have left for a textbox. Here is the code, is anyone savy...
12
by: Nileshs | last post by:
I have a page on which i need to show a pop up to the user if he tries to exit the page . I have already written an onunload function which takes care of displaying the popup . But the popup should...
5
by: anEchteTrilingue | last post by:
Hi everybody. Thank you for reading my post. I am having trouble getting "this" to work in all versions of IE (it's fine in Firefox, opera, konqueror, etc). What I would like to do is add an...
4
by: taygolf | last post by:
hey guys, I got a problem with srcElement and ie. I have a function called dropdown. What it is supposed to do is take the onmousedown event and get the select option from the drop down menu and...
5
by: GarryJones | last post by:
To show users how many characters they have left in a TEXTAREA input I have been using "taCount" from a website I googled. function taCount(visCnt) { var taObj=event.srcElement; if...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.