I personally use the wrapper function to handle manually generated events. The following code will add a static method for all Event interfaces (all global variables ending in Event are the Event interface) and allow you to call functions such as element.dispatchEvent(MouseEvent.create('click')); on IE9 +.
(function eventCreatorWrapper(eventClassNames){ eventClassNames.forEach(function(eventClassName){ window[eventClassName].createEvent = function(type,bubbles,cancelable){ var evt try{ evt = new window[eventClassName](type,{ bubbles: bubbles, cancelable: cancelable }); } catch (e){ evt = document.createEvent(eventClassName); evt.initEvent(type,bubbles,cancelable); } finally { return evt; } } }); }(function(root){ return Object.getOwnPropertyNames(root).filter(function(propertyName){ return /Event$/.test(propertyName) }); }(window)));
EDIT: the search function of all Event interfaces can also be replaced with an array to change only the necessary event interfaces ( ['Event', 'MouseEvent', 'KeyboardEvent', 'UIEvent'/*, etc... */] ).
Kevin Drost Feb 09 '18 at 11:57 2018-02-09 11:57
source share