Posted on 2006-04-21
Last Modified: 2012-05-05
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?
Question by:chsalvia
    1 Comment
    LVL 9

    Accepted 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 ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (, typical array handling might look like this: (CODE) B…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now