Path: csiph.com!aioe.org!news.mixmin.net!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: custom event in IE Date: Wed, 11 Jan 2017 08:25:24 +0100 Organization: PointedEars Software (PES) Lines: 45 Message-ID: <3041725.QJadu78ljV@PointedEars.de> References: <8bydnVoLx8BUq-nFnZ2dnUU7-bednZ2d@westnet.com.au> <4259298.GXAFRqVoOG@PointedEars.de> <2568174.e9J7NaK4W3@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1484119526 22804 eJwFwQEBwDAIAzBLA9oCdsaOfwlPGDJNQhS4XLU+PwgrXHvVcouM7MlH3NEUDgdYU7TgP/WxD5M= (11 Jan 2017 07:25:26 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Wed, 11 Jan 2017 07:25:26 +0000 (UTC) User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Because not only “new CustomEvent(theEvent)” may throw an exception, you > should not simply handle all exceptions caused by this statement with the > catch code, but only the specific one. Since there is no compatible way > to catch only a specific type of exception (AFAIK only Mozilla JavaScript > has a way, see the ES Matrix), you should rethrow the exception if it is > not the one that you want to handle. For example: > > try { > elem.dispatchEvent(new CustomEvent(theEvent)); > } catch (err) { > if (!(err instanceof TypeError)) throw err; > > var evt = document.createEvent("CustomEvent"); > evt.initCustomEvent(theEvent, true, false, {}); > elem.dispatchEvent(evt); > } Also, DRY: var evt = null; try { evt = new CustomEvent(eventType); } catch (err) { if (!(err instanceof TypeError)) throw err; evt = document.createEvent("CustomEvent"); if (evt) evt.initCustomEvent(eventType, true, false, {}); } if (evt) { try { elem.dispatchEvent(evt); } catch (e) { console.error(e); } } -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.