Re: Problem with Onclick in Netscape in <IMG tag

I agree with the comments posted by others, regarding avoiding javascript if
you can do it with html. INPUT TYPE="image" elements are probably the best
way to go.

Nevertheless, now that all the preaching has died down... ;-j
you may still be interested in knowing how to do it.

Your problem is that the IMG tag doesn't have an onClick event defined in
Netscape, only in IE. Instead, you can use the onClick of the A tag.

you'll find that this code works on both browsers:

<script language="Javascript">
function go()
{
  document.forms["main"].action="http://server/cgi-script";
  document.forms["main"].method="POST";
  document.forms["main"].submit();
}
</script>

<form name="main">
   <a href="" onClick = "go();return false"><IMG SCR="image.jpg"
border="0"></a>
   <input type="text">
</form>

You will need to return false from the onClick handler to prevent jumping to
the href url of the A tag.
Also, remeber to set the IMG border to 0 and don't put any white space
inside the A tag.

you can also eliminate a fair bit of the code:

<form name="main" action="http://server/cgi-script" method="POST">
   <a href="" onClick = "forms['main'].submit();return false"><IMG
SCR="image.jpg" border="0"></a>
   <input type="text">
</form>

...john


----- Original Message -----
From: "Mukul Gandhi" <mukulw3@yahoo.com>
To: <www-talk@w3.org>
Sent: 05 September 2000 17:03
Subject: Problem with Onclick in Netscape in <IMG tag


: Hi ,
: I have written a JavaScript method in HTML file which
: performs HTTP post operation as shown below -
: function go()
: {
:   document.form.action="http://server/cgi-script";
:   document.form.method="POST";
:   document.form.submit();
: }
:
: Then I am invoking this JavaScript method in HTML page
: as -
: <IMG SCR="image.jpg" onClick = 'go()'> . This works
: fine in IE 5, but in Netscape 4.7 it does,nt work. Is
: there any workaround, when I can do this operation in
: both the browsers(IE 4 and above + Netscape 4 and
: above).
: I have to perform POST only and cannot make this image
: as a link and do GET operation.
:
: Pl reply at mgandhi@bhartitelesoft.com
:
: regards
: -mukul
:
: __________________________________________________
: Do You Yahoo!?
: Yahoo! Mail - Free email you can access from anywhere!
: http://mail.yahoo.com/
:
:

Received on Thursday, 7 September 2000 20:03:40 UTC