30

I have a trouble in this part:

var ex = {
  exampl: function(){
    var ref=window.event.target||window.event.srcElement; // here
    alert(ref.innerHTML); // (example)
  }
}

This function is called this way:

document.body.childNodes[0].addEventListener('mouseover',ex.exampl,true);

Only Firefox says that window.event isn't defined...

I don't know what to do, to make it work. It works very well in webkit browsers and opera (I can't check it in MSIE and I don't care about it).

Why does it happen?

0

6 Answers 6

22

try getting the event using the parameter passed (named e in this case). i tested this and both window.event and the e is supported in chrome.

try checking for both, whichever exists

var ex = {
  exampl: function(e){

    console.log(window.event);
    console.log(e);  

    //check if we have "e" or "window.event" and use them as "evt"
    var evt = e || window.event    

  }
}
6
  • Should I change my "addEventListener"?
    – anony_root
    Commented Mar 21, 2012 at 22:04
  • Thank you! It was very useful for me, because it now works in FF: exampl:function(e) {if (window.event && !e) e=window.event;var ref=e.target||e.srcElement;}.
    – anony_root
    Commented Mar 21, 2012 at 22:11
  • 3
    But evt is not the same compared in chrome and FF. So this answer is not correct since I'd expect the same response...
    – Jonathan
    Commented Jul 16, 2015 at 14:33
  • I don't get it - wouldn't Chrome/IE also be providing e? why would you not just use e always Commented Jul 31, 2015 at 21:35
  • @Simon_Weaver Because Chrome and IE aren't the only browsers in the world, and the latest version isn't the only version the world is using. Firefox doesn't have window.event, IE doesn't have the first argument in the handler... or at least 3 years ago.
    – Joseph
    Commented Jul 31, 2015 at 22:27
19

window.event is not a feature, it's a bug!

Quoting MDN:

window.event is a proprietary Microsoft Internet Explorer property which is only available while a DOM event handler is being called. Its value is the Event object currently being handled.

And most importantly:

Not part of any specification.

window.event is non-standard, so don't expect any browsers to support it.

First parameter of callback function in element.addEventListener() is an Event object. Use it instead of window.event.

1
  • 1
    This pointed me in the right direction! I needed to get both the click event and a secondary argument. As you say the first argument of the function must be the event, and you need to include that in your call to the function as well; as in onclick='myClickEvent(event, mySecondAttr);'
    – SteinIP
    Commented May 27, 2016 at 8:20
15

Because window.event doesn't exist in Firefox. That's because browser have different event models and you'll have to deal with their differences or use a library like jQuery not to have to deal with all the differences between browsers. Welcome to the DOM.

0
5

Chrome doesn't have it natively either. Rather than having every event trigger pass its own event object, IE dropped properties into window.event and handed that off. This worked since you're only ever dealing with one event at a time. What every browser should have is window.Event which is the actual constructor for event objects. If you're seeing 'window.event' anywhere but IE8 and below, try opening a console on a blank tab and logging or alerting it there. Chances are something is adding it manually on the page you're looking at.

If you look at crossbrowser normalizing code for event handlers, you'll often see:

if(!e){ e = window.event; }

That's event normalizing code handling older versions of IE. In modern browsers, e should be its own object being passed through arguments, not a reference to a property of window.

0

use jQuery..

let e = $.Event();

https://api.jquery.com/category/events/

1
  • Instead of just providing reference link, please also provide an appropriate explanation as to how/why the above code will resolve the issue. Commented Nov 23, 2018 at 5:55
-1

window.event is available on Firefox from version 66 (release expected late octobre 2018).

3
  • Just started causing issues on our production server - since although it's there, it doesn't have a setable returnValue (which was there for ie 8)
    – Worthy7
    Commented Nov 21, 2018 at 5:57
  • nope is a lie 2023 still don't have event LOL
    – user956584
    Commented Feb 18, 2023 at 5:36
  • My Firefox 108 has it. Commented Feb 18, 2023 at 9:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.