Connecting Tech Pros Worldwide Forums | Help | Site Map

Does Event handlers work in netscape.

chandramohan.mani@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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 :((


paul.andrei@gmail.com
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Does Event handlers work in netscape.


testing

Dietmar Meier
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Does Event handlers work in netscape.


chandramohan.mani@gmail.com wrote:
[color=blue]
> The above script works fine in IE
> But not in Netscape 7.2 :(([/color]

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
RobG
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Does Event handlers work in netscape.


Dietmar Meier wrote:[color=blue]
> chandramohan.mani@gmail.com wrote:
>[color=green]
>> The above script works fine in IE
>> But not in Netscape 7.2 :(([/color]
>
>
> Try this modification:
>
> function mouseclick(e) {[/color]

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:
[color=blue]
> var what = e.target || e.srcElement;[/color]
[...]

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.


--
Rob
Martin Honnen
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Does Event handlers work in netscape.




chandramohan.mani@gmail.com wrote:
[color=blue]
> Does Event handlers work in netscape.[/color]

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.
[color=blue]
> <HTML><SCRIPT LANGUAGE="JScript">
> function mouseclick() {
> alert("I was clicked on " + window.event.srcElement.tagName);[/color]

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

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/default.asp?url=/workshop/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).


--

Martin Honnen
http://JavaScript.FAQTs.com/
Martin Honnen
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Does Event handlers work in netscape.




RobG wrote:
[color=blue]
> Dietmar Meier wrote:
>[/color]
[color=blue][color=green]
>> Try this modification:
>>
>> function mouseclick(e) {[/color]
>
>
> 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:
>[color=green]
>> var what = e.target || e.srcElement;[/color][/color]
[color=blue]
> You may only need the extra line if you don't pass 'event' with the
> onclick call.[/color]

And Dietmar had
<body onclick="mouseclick(event)">
in his answer so your line is not needed with his solution.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Richard Cornford
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Does Event handlers work in netscape.


Martin Honnen wrote:[color=blue]
> chandramohan.mani@gmail.com wrote:[/color]
<snip>[color=blue][color=green]
>> <HTML><SCRIPT LANGUAGE="JScript">[/color][/color]
^^^^^^^
<snip>[color=blue]
> ... but
> that your script being called then throws an error.[/color]
<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.


chandramohan.mani@gmail.com
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Does Event handlers work in netscape.


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

Dietmar Meier
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Does Event handlers work in netscape.


chandramohan.mani@gmail.com wrote:
[color=blue]
> Thanks for your code. But its not working in Netscape, same problem[/color]

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
Martin Honnen
Guest
 
Posts: n/a
#10: Jul 23 '05

re: Does Event handlers work in netscape.




Richard Cornford wrote:

[color=blue][color=green]
>>chandramohan.mani@gmail.com wrote:[/color]
>
>[color=green][color=darkred]
>>><HTML><SCRIPT LANGUAGE="JScript">[/color][/color]
>
> ^^^^^^^
>
> The script certainly would error if executed in Netscape/Mozilla, but
> would they execute script specified as language="JSCRIPT" in the first
> palace?[/color]

Good catch, Mozilla indeed ignores such a script block.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Closed Thread