Problem with "this" keyword in event handler of body element

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

    Problem with "this" keyword in event handler of body element

    Hi,

    I've got this very simple code:

    <body onclick="foo(ev ent, this)">

    Inside foo method in IE "this" is body object, but in FF it's window
    object - at least that's what MS Script Editor and Firebug are saying.
    Any ideas why it works like that?
  • RobG

    #2
    Re: Problem with &quot;this&quot ; keyword in event handler of body element

    szimek wrote:
    >> On 12 Lut, 12:04, szimek <szi...@gmail.c omwrote:
    >> >
    >> Hi,
    >> >
    >> I've got this very simple code:
    >> >
    >> <body onclick="foo(ev ent, this)">
    >> >
    >> Inside foo method in IE "this" is body object, but in FF it's window
    >> object - at least that's what MS Script Editor and Firebug are saying.
    [...]
    >
    I wrote this simple test page:
    <html>
    <body onclick="foo(th is);">
    <h1 onclick="foo(th is);">Test</h1>
    <script>
    function foo(element) {
    alert(element.t agName);
    }
    </script>
    </body>
    </html>
    >
    I tested it with FF 2.0.0.12 and 1.5.0.12 and both browsers give "H1"
    correctly when clicking on the header and "undefined" (window object)
    when clicking on the body. IE and Opera 9.23 work correctly giving
    "BODY" in the second case. Is it supposed to work like this or is it
    some long standing FF bug?
    Weird, I don't remember coming across that before, it seems like a bug
    in Firefox to me. I'll check Bugzilla later if I have time.



    --
    Rob
    "We shall not cease from exploration, and the end of all our
    exploring will be to arrive where we started and know the
    place for the first time." -- T. S. Eliot

    Comment

    • dhtml

      #3
      Re: Problem with &quot;this&quot ; keyword in event handler of body element

      On Feb 12, 1:04 pm, RobG <rg...@iinet.ne t.auwrote:
      szimek wrote:
      > On 12 Lut, 12:04, szimek <szi...@gmail.c omwrote:
      >
      > Hi,
      >
      > I've got this very simple code:
      >
      > <body onclick="foo(ev ent, this)">
      >
      > Inside foo method in IE "this" is body object, but in FF it's window
      > object - at least that's what MS Script Editor and Firebug are saying.
      [...]
      >
      I wrote this simple test page:
      <html>
      <body onclick="foo(th is);">
      <h1 onclick="foo(th is);">Test</h1>
      <script>
      function foo(element) {
      alert(element.t agName);
      }
      </script>
      </body>
      </html>
      >
      I tested it with FF 2.0.0.12 and 1.5.0.12 and both browsers give "H1"
      correctly when clicking on the header and "undefined" (window object)
      when clicking on the body. IE and Opera 9.23 work correctly giving
      "BODY" in the second case. Is it supposed to work like this or is it
      some long standing FF bug?
      >
      Weird, I don't remember coming across that before, it seems like a bug
      in Firefox to me. I'll check Bugzilla later if I have time.
      >
      It is a bug. I could not find a bug rep't.
      --
      Rob
      "We shall not cease from exploration, and the end of all our
      exploring will be to arrive where we started and know the
      place for the first time." -- T. S. Eliot

      Comment

      • RobG

        #4
        Re: Problem with &quot;this&quot ; keyword in event handler of body element

        On Feb 13, 4:14 pm, dhtml <dhtmlkitc...@g mail.comwrote:
        On Feb 12, 1:04 pm, RobG <rg...@iinet.ne t.auwrote:
        >
        szimek wrote:
        >> On 12 Lut, 12:04, szimek <szi...@gmail.c omwrote:
        >
        >> Hi,
        >
        >> I've got this very simple code:
        >
        >> <body onclick="foo(ev ent, this)">
        >
        >> Inside foo method in IE "this" is body object, but in FF it's window
        >> object - at least that's what MS Script Editor and Firebug are saying.
        [...]
        >
        I wrote this simple test page:
        <html>
        <body onclick="foo(th is);">
        <h1 onclick="foo(th is);">Test</h1>
        <script>
        function foo(element) {
        alert(element.t agName);
        }
        </script>
        </body>
        </html>
        >
        I tested it with FF 2.0.0.12 and 1.5.0.12 and both browsers give "H1"
        correctly when clicking on the header and "undefined" (window object)
        when clicking on the body. IE and Opera 9.23 work correctly giving
        "BODY" in the second case. Is it supposed to work like this or is it
        some long standing FF bug?
        >
        Weird, I don't remember coming across that before, it seems like a bug
        in Firefox to me. I'll check Bugzilla later if I have time.
        >
        It is a bug. I could not find a bug rep't.
        szimek seems to have reported it in Bugzilla ID #417080. I gave it a
        vote. :-)


        --
        Rob


        Comment

        • VK

          #5
          Re: Problem with &quot;this&quot ; keyword in event handler of body element

          On Feb 13, 9:48 am, RobG <rg...@iinet.ne t.auwrote:
          On Feb 13, 4:14 pm, dhtml <dhtmlkitc...@g mail.comwrote:
          >
          >
          >
          On Feb 12, 1:04 pm, RobG <rg...@iinet.ne t.auwrote:
          >
          szimek wrote:
          > On 12 Lut, 12:04, szimek <szi...@gmail.c omwrote:
          >
          > Hi,
          >
          > I've got this very simple code:
          >
          > <body onclick="foo(ev ent, this)">
          >
          > Inside foo method in IE "this" is body object, but in FF it's window
          > object - at least that's what MS Script Editor and Firebug are saying.
          [...]
          >
          I wrote this simple test page:
          <html>
          <body onclick="foo(th is);">
          <h1 onclick="foo(th is);">Test</h1>
          <script>
          function foo(element) {
          alert(element.t agName);
          }
          </script>
          </body>
          </html>
          >
          I tested it with FF 2.0.0.12 and 1.5.0.12 and both browsers give "H1"
          correctly when clicking on the header and "undefined" (window object)
          when clicking on the body. IE and Opera 9.23 work correctly giving
          "BODY" in the second case. Is it supposed to work like this or is it
          some long standing FF bug?
          >
          Weird, I don't remember coming across that before, it seems like a bug
          in Firefox to me. I'll check Bugzilla later if I have time.
          >
          It is a bug. I could not find a bug rep't.
          >
          szimek seems to have reported it in Bugzilla ID #417080. I gave it a
          vote. :-)
          I left my comment with the explanation of the phenomenon and voted NO.
          I believe Gecko behavior is fully consistent here with DOM 0.

          Comment

          • RobG

            #6
            Re: Problem with &quot;this&quot ; keyword in event handler of body element

            On Feb 14, 8:17 am, VK <schools_r...@y ahoo.comwrote:
            [...]
            I left my comment with the explanation of the phenomenon and voted NO.
            I believe Gecko behavior is fully consistent here with DOM 0.
            I think your logic is flawed, DOM 0 is not a specifictation, your
            reference to the onload attribute is a red herring. The body element
            is not the window object.

            The body element is defined in the HTML specification, it has an
            onclick attribute as do most other elements. Its behaviour in the DOM
            should be implemented exactly the same way as it is implemented on all
            other elements that have it, and as all other 'on' events are
            implemented.


            --
            Rob

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Problem with &quot;this&quot ; keyword in event handler of body element

              VK wrote:
              On Feb 13, 9:48 am, RobG <rg...@iinet.ne t.auwrote:
              >On Feb 13, 4:14 pm, dhtml <dhtmlkitc...@g mail.comwrote:
              >>It is a bug. I could not find a bug rep't.
              >szimek seems to have reported it in Bugzilla ID #417080. I gave it a
              >vote. :-)
              >
              I left my comment with the explanation of the phenomenon and voted NO.
              You have not even understood that it is not possible to vote NO on Bugzilla.
              I believe Gecko behavior is fully consistent here with DOM 0.
              I agree with RobG here, see my comment.


              PointedEars

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Problem with &quot;this&quot ; keyword in event handler of body element

                RobG wrote:
                On Feb 14, 8:17 am, VK <schools_r...@y ahoo.comwrote:
                >I left my comment with the explanation of the phenomenon and voted NO.
                >I believe Gecko behavior is fully consistent here with DOM 0.
                >
                I think your logic is flawed, DOM 0 is not a specifictation, your
                reference to the onload attribute is a red herring.
                He ridiculously referred to the `onload' *property*, _not_ attribute.

                Otherwise, full ACK.


                PointedEars
                --
                var bugRiddenCrashP ronePieceOfJunk = (
                navigator.userA gent.indexOf('M SIE 5') != -1
                && navigator.userA gent.indexOf('M ac') != -1
                ) // Plone, register_functi on.js:16

                Comment

                Working...