Majid
Majid

Reputation: 14253

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?

Upvotes: 13

Views: 48387

Answers (7)

cнŝdk
cнŝdk

Reputation: 32175

Attaching the event is a solution but there's an alternative one that could be helpful for other people facing the same problem, after a long search I found out that:

Event object is only reconized by firefox if you send explicitly the 'event' from the function

So the problem occurs because window.event is not recognized by Firefox, and the solution is to pass event to the function, your code will be :

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

And you can still call it in your inline HTML passing event as a parameter:

onclick="postBackByObject(event);"

Upvotes: 2

Deian Motov
Deian Motov

Reputation: 31

Pass the event on the onClick event like this:

onclick="myCustomFunction(event);"

It does work and you can access the event object!

Upvotes: 3

Dmitry
Dmitry

Reputation: 59

It's possible to pass event object in the inline declaration:

<input type="button" onclick="postBackByObject(event);" value="click" />

function postBackByObject(e) {
    var o = e.srcElement || e.target;
    // ...
}

Upvotes: 2

c.P.u1
c.P.u1

Reputation: 17094

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.

Upvotes: 7

HBP
HBP

Reputation: 16063

Your line

var o = window.event.srcElement || e.target;

fails on all browsers excepting IE, since for them windows.event is undefned

The proper formulation would be :

var o = e ? e.target : window.event.srcElement;

Since standard compliant browsers will pass the event as the parameter e and the target at e.target in IE, e will be undefined and you must use window.event.srcElement

Note that recent versions of IE do support the standards compliant model.

On a more generic note, when you try and access a value as a.b.c.d then a.b.c must be a defined object else you will get the error a.b.c undefined.

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

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;
    // ...
}

Upvotes: 15

amit
amit

Reputation: 337

i am not sure if window.event is supported in all browsers. I think you get event in variable "e" of postBackByObject(e)

Upvotes: 0

Related Questions