Anyone have any ideas why object.addEventListener does not work in FireFox and chrome?

DrewW
Mega Sage
Mega Sage

I have a client side script that I am using for masked text box editing so that the user will enter things like a phone number in the correct format as they type. For some reason when I do a obj.addEventListener('onfocus', function(event){ __OnMaskEditFocus(event); }, false); the onfocus event does not fire but if I do obj.onfocus = function(event){ __OnMaskEditFocus(event); }; the onfocus event will fire. I have looked up the syntax and as far as I can tell it looks right and when it runs there are no run time errors. Anyone have any ideas why? I would really love to know.


if(obj.addEventListener) {
     /*--
     obj.addEventListener('onkeydown', function(event){ __MaskEditDispatcher(event); }, false);
     obj.addEventListener('onkeyup', function(event){ __SetCursorPosition(event); }, false);
     obj.addEventListener('onkeypress', function(event){ __OnMaskEditUpdated(event); }, false);
     obj.addEventListener('onfocus', function(event){ __OnMaskEditFocus(event); }, false);
     --*/
     obj.onkeydown = function(event){ __MaskEditDispatcher(event); };
     obj.onkeyup = function(event){ __SetCursorPosition(event); };
     obj.onkeypress = function(event){ __OnMaskEditUpdated(event); };
     obj.onfocus = function(event){ __OnMaskEditFocus(event); };
} else if(obj.attachEvent) {
     obj.attachEvent('onkeydown', function () { eval('__MaskEditDispatcher(event);'); });
     obj.attachEvent('onkeyup', function () { eval('__SetCursorPosition(event);'); });
     obj.attachEvent('onkeypress', function () { eval('__OnMaskEditUpdated(event);'); });
     obj.attachEvent('onfocus', function () { eval('__OnMaskEditFocus(event);'); });
}

2 REPLIES 2

Daryll Conway
Giga Guru

I've had a similar issue with the addEventListener. I keep getting undefined returns when using Chrome but using other browsers there is no problem. Does anyone know a solution or work around for this issue?


Not sure this helps but I did come up with this for adding a dynamic onChange function to a field on a form.




      function onChange_sys_user(control, oldValue, newValue, isLoading) {


              if(isLoading)


                      return;


   


              if(newValue != "" $[AMP]$[AMP] $("message_area_hfhs") != null){


                      var ga = new GlideAjax("GetIncidentMessage");


                      ga.addParam("sysparm_name", "getMessage");


                      ga.addParam("sysparm_table", "sys_user");


                      ga.addParam("sysparm_filter_value", newValue);


                      ga.addParam("sysparm_filter_field", "sys_id");


                      ga.getXML(popupMessage);


              }


      }



      function onChange_cmn_location(control, oldValue, newValue, isLoading){


              if(isLoading)


                      return;



              if(newValue != "" $[AMP]$[AMP] $("message_area_hfhs") != null){


                      var ga = new GlideAjax("GetIncidentMessage");


                      ga.addParam("sysparm_name", "getMessage");


                      ga.addParam("sysparm_table", "cmn_location");


                      ga.addParam("sysparm_filter_value", newValue);


                      ga.addParam("sysparm_filter_field", "sys_id");


                      ga.getXML(popupMessage);


              }


      }



      function onChange_cmdb_ci(control, oldValue, newValue, isLoading){


              if(isLoading)


                      return;



              if(newValue != "" $[AMP]$[AMP] $("message_area_hfhs") != null){


                      var ga = new GlideAjax("GetIncidentMessage");


                      ga.addParam("sysparm_name", "getMessage");


                      ga.addParam("sysparm_table", "cmdb_ci");


                      ga.addParam("sysparm_filter_value", newValue);


                      ga.addParam("sysparm_filter_field", "sys_id");


                      ga.getXML(popupMessage);


              }


      }



      addLoadEvent(function () {



              var refFields = $j("input[data-type='ac_reference_input']");


              refFields.each(function(i, obj){


                      var thisHandler;


                      var handlerName = "";


                      var addHandler = true;


                      if(obj.getAttribute("data-table") == "sys_user"){


                              handlerName = "onChange_" + obj.getAttribute("data-ref") + "_hfhs_" + i;


                              thisHandler = new GlideEventHandler(handlerName, onChange_sys_user, obj.getAttribute("data-ref"));


                      } else if(obj.getAttribute("data-table") == "cmdb_ci"){


                              handlerName = "onChange_" + obj.getAttribute("data-ref") + "_hfhs_" + i;


                              thisHandler = new GlideEventHandler(handlerName, onChange_cmdb_ci, obj.getAttribute("data-ref"));


                      } else if(obj.getAttribute("data-table") == "cmn_location"){


                              handlerName = "onChange_" + obj.getAttribute("data-ref") + "_hfhs_" + i;


                              thisHandler = new GlideEventHandler(handlerName, onChange_cmn_location, obj.getAttribute("data-ref"));


                      } else {


                              addHandler = false;


                      }


                      if(addHandler){


                              g_event_handlers_onChange[handlerName] = handlerName;


                              g_event_handler_ids[handlerName] = handlerName;


                              g_event_handlers.push(thisHandler);


                      }


              })


   


      });