Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'event' equivalent in Firefox

I am using the following code and it works perfectly fine in Chrome.

function dayBind(xyzValue) {
    if(event.type == 'click')
       alert('Mouse Clicked')
}

Note that there was no 'event' variable passed to the function but still it was available for me in case of chrome. But when I use Firefox I get 'event' undefined. I tried using the following workarounds:

var e=arguments[0] || event;

also:

var e=window.event || event;

But none of them worked for me. Is there any 'event' equivalent in Firefox?

like image 940
Adil Malik Avatar asked Mar 09 '12 15:03

Adil Malik


2 Answers

Because IE and Chrome put the event in the global object window, so you can get it. In firefox, you need to let the first parameter be the event.

function dayBind(event, xyzValue) {
    var e=event || window.event;
    if(event.type == 'click')
       alert('Mouse Clicked')
}
like image 170
xdazz Avatar answered Oct 01 '22 14:10

xdazz


If you're setting up the handler with an "onclick" attribute or something (which, since you tagged the question "jQuery", you really should consider not doing), you have to explicitly pass it:

<button type=button onclick='whatever(event)'>Click Me</button>
like image 32
Pointy Avatar answered Oct 01 '22 14:10

Pointy