2

Why do this to check if event is undefined...?

function onMouseMove(e) {

        if (typeof e === 'undefined' || typeof e.pageX === 'undefined') {
            return;
        }
1
  • If the function is used as event handler, there is no reason to check the argument. In this case, this looks like superstition : remove the test. Useless assertions don't prevent bugs : they just make the code harder to read and maintain. Commented Oct 22, 2012 at 17:19

5 Answers 5

3

That check stems from different browsers calling the event in their own ways. Some browsers do not pass in e and it is defined as the global event object that you must access each time a mouseHandler is called.

For example, the following code will always give you the event object regardless of browser:

e = e || window.event

For more details, check out the article found by raina77ow:

http://www.quirksmode.org/js/events_access.html

Sign up to request clarification or add additional context in comments.

5 Comments

Exactly (+1). But, well, the ones that don't pass it are really the OLD ones. )
do you have anything to back this up - like documentation on which browsers / versions do this? I've never encountered it before
Personal experience? I could find the documentation somewhere but for my own projects when I've had to make things work on IE 8 and below, I've had to use this. Not sure if the same problem still exists in IE9 or not...
@tomfumb Check this article. As I remember, it's about IE4 (or something like that), that didn't support attachEvent method of registering event handlers.
@TMan does e.window.event are equivalent to e.window.srcElement
2

If you are passing event object onMouseMove then you do not need to check. But This function could be called with passing arguments in that case e needs to be checked.

Live Demo

<div id="div1" onmousemove="onMouseMove(event)" > mouse move test, with event </div>

<div id="div1" onmousemove="onMouseMove()" > mouse move test, without event </div>​

function onMouseMove(e) {         
    if (typeof e === 'undefined' || typeof e.pageX === 'undefined') {
        alert("With e");
    }
    else
    {       
        alert("Without e");
    }        
}​

3 Comments

I'm sorry but in practice, when do you actually call you own onMouseMove event?
When we call we pass event but we are not bound to pass.
No, I understand that. I'm saying when do you call a mouseHandler or any event handler for that matter on your own?
2
if (typeof e === 'undefined' || typeof e.pageX === 'undefined')

its check if the even handler are underfined or not otherwise return anything else ..coz this is the ie issue.

Comments

0

The only thing I can think of to check if the function is being invoked by a real event in the browser, or being called by other JS code.

For example, this will execute when an event is fired, passing in an event object.

$('#someID').on('mousemove', onMouseMove);

However, this will not pass any arguments at all

onMouseMove();

Why you need the check in your code though is a little odd. You usually wouldn't invoke event handlers directly. So it's a little odd.

Comments

0

It is generally good practice to check your inputs, especially in a loosely-typed language like JavaScript. In this case we want to make sure the expected input is both defined, and if it is defined, we need to make sure it is an object with the property pageX. Simply checking the first clause of that conditional is insufficient because it may be missing pageX and only checking the second clause would throw an error if e was undefined entirely.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.