最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 'event' equivalent in Firefox - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Jan 1, 2021 at 18:04 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Mar 9, 2012 at 15:15 Adil MalikAdil Malik 6,3577 gold badges50 silver badges80 bronze badges 4
  • 1 How is the event listener registered? – David Hedlund Commented Mar 9, 2012 at 15:17
  • 2 How is dayBind called? – josh3736 Commented Mar 9, 2012 at 15:17
  • 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. – Cfreak Commented Mar 9, 2012 at 15:18
  • 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. – Adil Malik Commented Mar 9, 2012 at 19:05
Add a comment  | 

4 Answers 4

Reset to default 11

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

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>

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

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

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

References

  • Overview of Events and Handlers
  • DOM Event Handlers
  • eventTarget.addEventListener

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.

发布评论

评论列表(0)

  1. 暂无评论