Netscape won't change styles in functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 2obvious

    Netscape won't change styles in functions

    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.getEle mentById("blah" ).style.color = "green";
    }

    if ( window.attachEv ent )
    window.attachEv ent("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.
  • Martin Honnen

    #2
    Re: Netscape won't change styles in functions



    2obvious wrote:
    [color=blue]
    > 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.getEle mentById("blah" ).style.color = "green";
    > }
    >
    > if ( window.attachEv ent )
    > window.attachEv ent("onload", test);[/color]

    You need
    else if (window.addEven tListener) {
    window.addEvent Listener(
    '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.

    --

    Martin Honnen


    Comment

    • Yann-Erwan Perio

      #3
      Re: Netscape won't change styles in functions

      2obvious wrote:
      [color=blue]
      > Below is a code example. On a PC, it works in IE6. It does not work
      > in Netscapes 6 or 7. Why?[/color]
      [color=blue]
      > if ( window.attachEv ent )
      > window.attachEv ent("onload", test);[/color]

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

      <URL:http://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#Eve nts-EventTarget-addEventListene r>

      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 addEventListene r or attachEvent; try to read and
      learn from:

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


      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.

      Comment

      • 2obvious

        #4
        Re: Netscape won't change styles in functions

        Wow. Much thanks. See? Simple.
        [color=blue]
        > 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.[/color]

        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.

        Comment

        Working...