Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox "window.event is undefined" error

I have this script:

function postBackByObject(e) {
   var o = window.event.srcElement || e.target;
   if (o.tagName == "INPUT" && o.type == "checkbox") {
        __doPostBack("", "");
    }
}

I use this script with onclick="postBackByObject();".

but in Firefox 21 I get this error:

TypeError: window.event is undefined

what is my wrong?

like image 807
Majid Avatar asked Jun 25 '13 11:06

Majid


3 Answers

You are attaching events inline onclick="postBackByObject();"

Try passing this(the event target) to onclick="postBackByObject(this);" Modify your function to handle this change:

function postBackByObject(e) {
   if (e.tagName == "INPUT" && e.type == "checkbox") {
        __doPostBack("", "");
    }
}

A better alternative will be to attach events using addEventListener

If your markup looks like:

<div id="TvCategories" onclick="postBackByObject(this);" />

then

document.getElementById('TvCategories').addEventListener('click', postBackByObject);

Your postBackByObject function remains unchanged when using this approach.

like image 146
c.P.u1 Avatar answered Nov 07 '22 00:11

c.P.u1


That's because it is. window.event is for older versions of IE.

The typical way to do this is:

function postBackByObject(e) {
    e = e || window.event;
    var o = e.srcElement || e.target;
    // ...
}
like image 15
Niet the Dark Absol Avatar answered Nov 06 '22 23:11

Niet the Dark Absol


Pass the event on the onClick event like this:

onclick="myCustomFunction(event);"

It does work and you can access the event object!

like image 3
Deian Motov Avatar answered Nov 06 '22 23:11

Deian Motov