Netscape won't change styles in functions

2

2obvious

This is nice and simple:

Below is a code example. On a PC, it works in IE6. It does not work
in Netscapes 6 or 7. Why?

<html><head>
<script>
function test()
{
document.getElementById("blah").style.color = "green";
}

if ( window.attachEvent )
window.attachEvent("onload", test);
</script>
</head><body>

<h1 id="blah">Test</h1>

</body></html>

In more depth: I am trying to test my ability to set styles from
inside functions on Netscape. The situation that inspired my example
code worked in NN7 but not NN6. I'm noticing that my example,
however, works in neither.

I'd sure like to know why. Bad syntax on my part? Bugs in Netscape?
A way to make it work? Thanks.
 
M

Martin Honnen

2obvious said:
This is nice and simple:

Below is a code example. On a PC, it works in IE6. It does not work
in Netscapes 6 or 7. Why?

<html><head>
<script>
function test()
{
document.getElementById("blah").style.color = "green";
}

if ( window.attachEvent )
window.attachEvent("onload", test);

You need
else if (window.addEventListener) {
window.addEventListener(
'load',
test,
false
);
}
to have code that works with W3C DOM compliant browsers like Mozilla,
Netscape 6/7, Firefox. The attachEvent method is part of IE 5.5/6 event
model.
 
Y

Yann-Erwan Perio

2obvious said:
Below is a code example. On a PC, it works in IE6. It does not work
in Netscapes 6 or 7. Why?
if ( window.attachEvent )
window.attachEvent("onload", test);

The "attachEvent" method is proprietary to IE; the equivalent DOM
standard method, used by Mozilla, Opera and every DOM 2 Events compliant
browsers, is "addEventListener".

<URL:http://www.w3.org/TR/2003/NOTE-DOM-...ents.html#Events-EventTarget-addEventListener>

You'll find out that IE's events model and W3C's events models differ
greatly, IE's model being more limited (no event capture, no
namespace-based methods, no custom event management, useless "this"
value with attachEvent...).

There are also ways to use multiple event listeners on an element
without resorting to addEventListener or attachEvent; try to read and
learn from:

<URL:http://www.infimum.dk/privat/eventListener.js>


If I may suggest, you should include the following step in your
debugging process: when you have a piece of code which doesn't seem to
work, run the piece of code manually instead of relying on the caller
context, setting yourself manually any necessary input; this removes the
caller context from the equation and permits to assert that the error
really comes from the piece of code.


HTH
Yep.
 
2

2obvious

Wow. Much thanks. See? Simple.
If I may suggest, you should include the following step in your
debugging process: when you have a piece of code which doesn't seem to
work, run the piece of code manually instead of relying on the caller
context, setting yourself manually any necessary input; this removes the
caller context from the equation and permits to assert that the error
really comes from the piece of code.

Actually, I /did/ test the code [inside the function] directly, before
putting it in the function. This is why I thought the putting it in
the function part was causing the problem.

Now, I failed to call the _function_ directly, to test if my technique
for calling functions was the problem. (--Wait. Maybe that's what
you were just saying?) Either way, lessoned learned. Thank you
muchly for your detailed explanation, Yan. And Martin, thank you for
your straightforward code snippet.

--E.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top