Javascript: vanilla and crossbrowser event handler accepting arguments


Sun Aug 28 2022 23:02:59 GMT+0000 (Coordinated Universal Time)

Saved by @marcopinero #javascript

function EventHandler(arg1, arg2,...) {
    var evt = window.event || function(cx) {
    if(typeof Event!='undefined' && cx){
    	for(var ii=0; ii<cx.arguments.length; ii++)
    		if(cx.arguments[ii] instanceof Event) return cx.arguments[ii];
    		return arguments.callee(cx.caller);
    	return null;
    evt.preventDefault=evt.preventDefault||function(){evt.returnValue = false;};
    evt.stopPropagation=evt.stopPropagation||function(){evt.cancelBubble = false;};

This is a template for defining javascript event handler with additional parameters support and stopPropagation/preventDefault handling.