DOM: removeAttribute() in Internet Explorer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alvaro G Vicario

    #1

    DOM: removeAttribute() in Internet Explorer


    I need to dynamically remove the onmouseout attribute from a link in an
    HTML page. I've written code that uses the removeAttribute () method,
    believing it's a W3C standard. While it works fine in Mozilla, it doesn't
    work as expected in Internet Explorer 6.

    If I call the method several times, the first execution returns true and
    the rest return false, so it's apparently doing something. However, the
    attribute is still there: hovering the mouse triggers the code and
    getAttribute() displays the old value.

    How should I do it?

    Example code:

    <p><a id="foo" onmouseout="thi s.style.color=' green'"
    href="http://www.google.es"> Example link</a></p>

    <pre>
    <script language="JavaS cript" type="text/javascript"><!--
    var obj=document.ge tElementById('f oo');
    document.writel n(obj.getAttrib ute('onmouseout '));
    obj.removeAttri bute('onmouseou t');
    document.writel n(obj.getAttrib ute('onmouseout '));
    //--></script>
    </pre>



    Mozilla prints:

    this.style.colo r='green'
    null

    And link remains unchanged.


    Explorer prints:

    function anonymous()
    {
    this.style.colo r='green'
    }
    function anonymous()
    {
    this.style.colo r='green'
    }

    And link gets green.

    Thank you in advance,
  • Martin Honnen

    #2
    Re: DOM: removeAttribute () in Internet Explorer



    Alvaro G Vicario wrote:
    [color=blue]
    > I need to dynamically remove the onmouseout attribute from a link in an
    > HTML page. I've written code that uses the removeAttribute () method,
    > believing it's a W3C standard. While it works fine in Mozilla, it doesn't
    > work as expected in Internet Explorer 6.[/color]

    Unfortunately IE impements some methods with the same name of a method
    specified in the W3C DOM but IE's implementation is not what the W3C DOM
    specifies. get/setAttribute are in particular broken in IE when it comes
    to W3C DOM compliance.
    [color=blue]
    > If I call the method several times, the first execution returns true and
    > the rest return false, so it's apparently doing something. However, the
    > attribute is still there: hovering the mouse triggers the code and
    > getAttribute() displays the old value.
    >
    > How should I do it?[/color]
    [color=blue]
    > <p><a id="foo" onmouseout="thi s.style.color=' green'"
    > href="http://www.google.es"> Example link</a></p>[/color]
    [color=blue]
    > var obj=document.ge tElementById('f oo');[/color]

    In client-side script in HTML pages if you want to remove an event
    handler function assigned as oneventname (in HTML or per script) your
    best cross-browser way is
    elementObject.o neventname = null;
    e.g.
    obj.onmouseout = null;

    This does not completely remove the attribute but it removes the event
    handler.

    --

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

    Comment

    • Alvaro G Vicario

      #3
      Re: DOM: removeAttribute () in Internet Explorer

      *** Martin Honnen wrote/escribió (Mon, 14 Mar 2005 14:17:07 +0100):[color=blue]
      > In client-side script in HTML pages if you want to remove an event
      > handler function assigned as oneventname (in HTML or per script) your
      > best cross-browser way is
      > elementObject.o neventname = null;
      > e.g.
      > obj.onmouseout = null;
      >
      > This does not completely remove the attribute but it removes the event
      > handler.[/color]

      Works like a charm and it looks cross-browser enough. Thanks a lot.


      --
      -- Álvaro G. Vicario - Burgos, Spain
      -- Don't e-mail me your questions, post them to the group
      --

      Comment

      Working...