Does Event handlers work in netscape.

C

chandramohan.mani

Does Event handlers work in netscape. If yes means can anyone please
help me.

<HTML><SCRIPT LANGUAGE="JScript">
function mouseclick() {
alert("I was clicked on " + window.event.srcElement.tagName);
}
</SCRIPT>
<BODY onclick="mouseclick()">
<H1>Welcome!</H1>
<P>This is a very <B>short</B> document.

</BODY>
</HTML>

The above script works fine in IE

But not in Netscape 7.2 :((
 
D

Dietmar Meier

The above script works fine in IE
But not in Netscape 7.2 :((

Try this modification:

function mouseclick(e) {
var what = e.target || e.srcElement;
if (what) alert("I was clicked on " + what.tagName);
}
[...]
<body onclick="mouseclick(event)">

ciao, dhgm
 
R

RobG

Dietmar said:
Try this modification:

function mouseclick(e) {

if (!e && window.event) var e = window.event;

I'm not near an IE box right now, but I think to keep IE happy you'll
need to add the above line before:
var what = e.target || e.srcElement;
[...]

I'm playing in Safari at the moment, it seems to support a bit of both
Firefox/Netscape and IE (but often neither...) so I can't test it in
IE.

You may only need the extra line if you don't pass 'event' with the
onclick call.
 
M

Martin Honnen

Does Event handlers work in netscape.

Yes, event handlers work in Netscape, depending on the Netscape version
not all event handlers specified in HTML 4 might work but with Netscape
6/7 there is certainly support for HTML 4 and for DOM Level 2 Events.
<HTML><SCRIPT LANGUAGE="JScript">
function mouseclick() {
alert("I was clicked on " + window.event.srcElement.tagName);

Have you checked the JavaScript console? It probably shows you that
window.event
is undefined or not an object with Netscape 6/7
<BODY onclick="mouseclick()">

so the problem is not that the onclick handler is not being fired but
that your script being called then throws an error.
You can pass the event object to a function e.g.
<body onclick="mouseclick(event);">
and then you need to process that argument in the function e.g.
function mouseclick (evt) {
and then you need to be aware that properties of the event object differ
in IE and in Netscape, IE docs are here:
<http://msdn.microsoft.com/library/d.../author/dhtml/reference/objects/obj_event.asp>
Netscape 6/7 implements the W3C DOM as given here:
<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event>
<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-UIEvent>
<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-MouseEvent>

You already got an answer showing to access target with Netscape (or
other browser supporting that) and srcElement with IE (or other browsers
supporting that).
 
R

Richard Cornford

Martin said:
(e-mail address removed) wrote:
^^^^^^^
... but
that your script being called then throws an error.
<snip>

The script certainly would error if executed in Netscape/Mozilla, but
would they execute script specified as language="JSCRIPT" in the first
palace?

Richard.
 
D

Dietmar Meier

Thanks for your code. But its not working in Netscape, same problem

In addition to my code, see Richard's answer. Instead of

<SCRIPT LANGUAGE="JScript">

(which is MSIE-proprietary) you should use

<script type="text/javascript">

to have your code executed in both Netscape7 and MSIE (and
other browsers). A complete example document would be:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Script-Type"
content="text/javascript">
<meta http-equiv="content-type"
content="text/html; charset=ISO-8859-1">
<title></title>
<script type="text/javascript">
function mouseclick(e) {
var what = e.target || e.srcElement;
if (what) alert("I was clicked on " + what.tagName);
}
</script>
</head>
<body onclick="mouseclick(event)">
<H1>Welcome!</H1>
<P>This is a very <B>short</B> document.
</body>
</html>

ciao, dhgm
 
M

Martin Honnen

Richard Cornford wrote:

^^^^^^^

The script certainly would error if executed in Netscape/Mozilla, but
would they execute script specified as language="JSCRIPT" in the first
palace?

Good catch, Mozilla indeed ignores such a script block.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top