#383 ✓resolved
Mathias Karstädt

Event.element not working in IE if event.type is 'unload'

Reported by Mathias Karstädt | October 9th, 2008 @ 01:18 PM | in 1.6.1

Event.element is not working properly in IE because event.target and event.currentTarget are not supported in IE.

This is the code snipped I am talking about:


element: function(event) {
  event = Event.extend(event);

  var node          = event.target,
      type          = event.type,
      currentTarget = event.currentTarget;

  if (currentTarget && currentTarget.tagName) {
    // Firefox screws up the "click" event when moving between radio buttons
    // via arrow keys. It also screws up the "load" and "error" events on images,
    // reporting the document as the target instead of the original image.
    if (type === 'load' || type === 'error' ||
      (type === 'click' && currentTarget.tagName.toLowerCase() === 'input'
        && currentTarget.type === 'radio'))
          node = currentTarget;
  }
  if (node.nodeType == Node.TEXT_NODE) node = node.parentNode;
  return Element.extend(node);
},

In IE you have to use event.srcElement to get the target element of the event.

Comments and changes to this ticket

  • Mathias Karstädt

    Mathias Karstädt October 9th, 2008 @ 01:29 PM

    • Title changed from “Event.element and event.target not working in IE” to “Event.element not working in IE if event.type is 'unload'”

    sorry, I know that the event is extended by the Event object, so event.target is event.srcElement. So I was wrong with that!

    But if the Event.type is 'unload' and the returned node by event.target is null I get an error at this line: @@@javascript if (node.nodeType == Node.TEXT_NODE) node = node.parentNode;

    
    
    If node is null this throws an exception.
    
  • Tobie Langel

    Tobie Langel October 9th, 2008 @ 02:05 PM

    • State changed from “new” to “bug”
    • Milestone set to 1.6.1
    • Tag changed from 1.6.0.3, element, events, ie to 1.6.0.3, element, events, ie, needs_failing_test_case, needs_patch, needs_tests
  • Mathias Karstädt

    Mathias Karstädt October 9th, 2008 @ 03:05 PM

    I did a small test case. see attached file...

  • Tobie Langel

    Tobie Langel October 9th, 2008 @ 04:01 PM

    event.srcElement should never be null, so I guess that's what we should be fixing.

  • Diego Perini

    Diego Perini November 11th, 2008 @ 03:43 PM

    Tobie, it seems to me that the "event.srcElement" is "null" when you attach an event to "window". This is in IE obviously, and probably it is due to the fact that "window" is not a DOM element.

    Another bug that may show up here is that in Opera, using the "onload" event on the "window", the "this" keyword will reference the "document" instead of the original "window" where the event was setup. Don't know why, but seems an Opera bug to me.

    -- Diego

  • Andrew Dupont

    Andrew Dupont December 3rd, 2008 @ 01:38 AM

    • Assigned user set to “Andrew Dupont”
    • Tag changed from 1.6.0.3, element, events, ie, needs_failing_test_case, needs_patch, needs_tests to 1.6.0.3, discuss, element, events, ie, needs_failing_test_case, needs_patch, needs_tests

    I don't mind that event.srcElement is null in this case, since an unload event isn't really owned by any DOM object. But window is also a valid return value for Event.element here. I have a slight preference for null, but I don't really care either way.

    Any other opinions?

  • Diego Perini

    Diego Perini December 3rd, 2008 @ 01:55 AM

    Andrew, when fixing the event properties for IE in my events I always set the "target" of the event object to refer to the corresponding "srcElement" property in IE. If that is null I set it to the element the event was registered to listen on. I have:

    event.target = event.srcElement || element;

    a wrapper is passing the original registered "element" to my "fix()" method.

    I believe this fit Tobie request of ensuring it will never be null.

    -- Diego

  • GitHub Robot

    GitHub Robot February 22nd, 2009 @ 06:38 AM

    • State changed from “bug” to “resolved”

    (from [91b787ae177d5f57919d416955e8d8c85e79019d]) Ensure the target property on events is never undefined in IE. [#383 state:resolved] http://github.com/sstephenson/pr...

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:11 AM

    • Tag changed from 1.6.0.3, discuss, element, events, ie, needs_failing_test_case, needs_patch, needs_tests to 1.6.0.3, discuss, element, events, ie, needs_failing_test_case, needs_patch, needs_tests, section:dom

    [not-tagged:"dom" tagged:"section:dom" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:12 AM

    • Tag changed from 1.6.0.3, discuss, element, events, ie, needs_failing_test_case, needs_patch, needs_tests, section:dom to 1.6.0.3, discuss, events, ie, needs_failing_test_case, needs_patch, needs_tests, section:dom

    [not-tagged:"element" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:23 AM

    • Tag changed from 1.6.0.3, discuss, events, ie, needs_failing_test_case, needs_patch, needs_tests, section:dom to discuss, events, ie, needs_failing_test_case, needs_patch, needs_tests, section:dom

    [not-tagged:"1.6.0.3" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:26 AM

    • Tag changed from discuss, events, ie, needs_failing_test_case, needs_patch, needs_tests, section:dom to discuss, events, ie, missing:tests, needs_failing_test_case, needs_patch, section:dom

    [not-tagged:"needs_tests" tagged:"missing:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:28 AM

    • Tag changed from discuss, events, ie, missing:tests, needs_failing_test_case, needs_patch, section:dom to discuss, events, ie, missing:patch, missing:tests, needs_failing_test_case, section:dom

    [not-tagged:"needs_patch" tagged:"missing:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:36 AM

    • Tag changed from discuss, events, ie, missing:patch, missing:tests, needs_failing_test_case, section:dom to discuss, events, ie, missing:patch, needs:tests, needs_failing_test_case, section:dom

    [not-tagged:"missing:tests" tagged:"needs:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:37 AM

    • Tag changed from discuss, events, ie, missing:patch, needs:tests, needs_failing_test_case, section:dom to discuss, events, ie, needs:patch, needs:tests, needs_failing_test_case, section:dom

    [not-tagged:"missing:patch" tagged:"needs:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:39 AM

    • Tag changed from discuss, events, ie, needs:patch, needs:tests, needs_failing_test_case, section:dom to events, ie, needs:discussion, needs:patch, needs:tests, needs_failing_test_case, section:dom

    [not-tagged:"discuss" tagged:"needs:discussion" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:40 AM

    • Tag changed from events, ie, needs:discussion, needs:patch, needs:tests, needs_failing_test_case, section:dom to events, ie, needs:discussion, needs:failing_testcase, needs:patch, needs:tests, section:dom

    [not-tagged:"needs_failing_test_case" tagged:"needs:failing_testcase" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:43 AM

    • Tag changed from events, ie, needs:discussion, needs:failing_testcase, needs:patch, needs:tests, section:dom to ie, needs:discussion, needs:failing_testcase, needs:patch, needs:tests, section:dom

    [not-tagged:"events" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 04:05 AM

    • Tag changed from ie, needs:discussion, needs:failing_testcase, needs:patch, needs:tests, section:dom to needs:discussion, needs:failing_testcase, needs:patch, needs:tests, section:dom

    [not-tagged:"ie" not-tagged:"ie6" not-tagged:"ie7" not-tagged:"ie8" bulk edit command]

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins

Attachments

Referenced by

Pages