Firefox “window.event is undefined” error

后端 未结 7 1397
悲哀的现实
悲哀的现实 2020-12-03 11:38

I have this script:

function postBackByObject(e) {
   var o = window.event.srcElement || e.target;
   if (o.tagName == \"INPUT\" && o.type == \"check         


        
7条回答
  •  清歌不尽
    2020-12-03 12:08

    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.

提交回复
热议问题