'evento' equivalente en 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;

también:

var e=window.event || event;

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

preguntado el 09 de marzo de 12 a las 15:03

How is the event listener registered? -

¿Cómo es dayBind ¿llamada? -

post the rest of your code. Event should be passed to the function so it should be your xyzValue. However without some context it's hard to tell. -

This is how it's being called: dayBind(bodyCells); I am working on FullCalendar plugin. That's a lot of code. It's not possible to post it here. If you need more details to diagnose please let me know I'll give you all the details. -

4 Respuestas

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')
}

respondido 09 mar '12, 15:03

and what would the function call of 'dayBind' look like? I want to call this function from javascript where I have no variable of 'event' but I only have the xyzValue to pass to dayBind method. - Adil Malik

@AdilMalik If you use jQuery, why not use the .bind() which will save you a lot of time. jQuery will handle the event bind for cross browser. - xdazz

I am working in a very large and complicated code. I cannot use .bind() I just need to get event object in this function somehow. I wonder why is it so difficult in firefox :( - Adil Malik

parece el event.type in the third row of the code should be a e.type - davidtaubmann

Use "this" on entry to the routine and derive the event from it. - Ecualizador gráfico

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>

respondido 09 mar '12, 15:03

I am working in a plugin's callback function. I cannot call it myself. One simple question to your suggestion: when you write: onclick="whatever(event)" you are writing javascript in the value of onclick attribute, right? Why can't you make the same function call inside some other function like this: function foo(){ whatever(event); // this is also javascript } // But this doesn't work for me in FireFox 10.0.2 - Adil Malik

The code in the "onclick" attribute should be thought of as part of a function that the browser creates for you. That function automáticamente has the "event" parameter available. Writing the attribute as I did in the answer cause that parameter to be passed on the your other function. Really, you should read about the jQuery API and use that to bind event handlers instead of using "onclick" and other similar attributes. - Puntiagudo

If you need it to work cross browser, simply use the arguments :

function dayBind() 
  {
  var e=arguments[0];

  if(!!e && e.type === 'click')
    {
    alert('Mouse Clicked') 
    }
  }

Referencias

Respondido 05 Feb 16, 13:02

I am working in a plugin's callback function. I cannot call it myself.

One simple question to your suggestion: when you write: onclick="whatever(event)" you are writing javascript in the value of onclick attribute, right?

Why can't you make the same function call inside some other function like this:

function foo(){ whatever(event); // this is also javascript }

// But this doesn't work for me in FireFox 10.0.2

The code in the "onclick" attribute should be thought of as part of a function that the browser creates for you. That function automatically has the "event" parameter available. Writing the attribute as I did in the answer cause that parameter to be passed on the your other function.

Really, you should read about the jQuery API and use that to bind event handlers instead of using "onclick" and other similar attributes.

Respondido el 07 de Septiembre de 18 a las 06:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.