Smax Smaxović
Smax Smaxović

Reputation: 560

window.event alternative in Firefox

I see that window.event or event does not work in Firefox, so I need alternative for this. I don't want to set ANY HTML attributes, just Javascript. I'm within this function and I want to get mouse coordinates from here:

document.onmouseover = function(){
    var mouseX = event.clientX;
    var mouseY = event.clientY;
}

Obviously this won't work in firefox, so I want to know how to do it.

Upvotes: 13

Views: 17162

Answers (2)

cookie monster
cookie monster

Reputation: 10972

This is the typical approach that you'll find in examples everywhere.

document.onmouseover = function(event) {
    event = event || window.event;

    var mouseX = event.clientX;
    var mouseY = event.clientY;
}

The W3C standard way of retrieving the event object is via the first function parameter. Older IE didn't support that approach, so event will be undefined. The || operator lets us fetch the window.event object in that case.

Upvotes: 15

Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

window.event (which you're referring to) is a global object that isn't available in all browsers.

document.addEventListener("mouseover", function(evt){
    var mouseX = evt.clientX;
    var mouseY = evt.clientY;
}, false);

Upvotes: 0

Related Questions