[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2128
  • Last Modified:


Can someone give me an explanation of the following line:

Assuming a function like:

function mouseUp(ev)

What does this line mean:

ev = ev || window.event;

A lot of code snippets I see have an event variable as an argument to some event-based function, like mouseUp, mouseMove, or whatever.  But the event variable is never actually passed to the function - it's simply stated as an argument to the function, so essentially that means it would be undefined.

So, what does ev = ev || window.event mean?  It seems like this line tries to set ev to its own value, which is undefined, or to the value of window.event.  What does this line achieve?
1 Solution
For browsers other than IE, when a DOM event occurs, it calls the handler of that event (if any), and pass to this handler the event object.
In IE, it is not the case. In this browser, you acces the event object not through the argument of the function but with window.event object.
That's why you see this code. It's for cross browser compatibility.
In IE, the variable ev will be initialized with window.event. In other browsers like FF, it will use the argument passed to the function.

Hope am clear ;)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now