JS: Event depreciated warning in inspector
HI I have this piece of code:
// in editor the "event" is stroked through
// indicating that it is causing some problem
<div onclick="event.stopPropagation();">
In PHPstorm the inspection "JavaScript -> General -> Deprecated Java Script symbol" is triggered and it suggesting to refactor it. How should I change it?
Tried to find solution, but after few hours I have nothing - any suggestions?
Please sign in to leave a comment.
what is event in your code, where is it defined? Seems no definition can be found in your code, so the IDE resolves it to
in lib.dom.d.ts where it is, indeed, marked deprecated
To my best understanding the "event" is a reserved JS keyword, for inline functions on html elements are used to pass the details of whatever happened (e.g. key or mouse button pressed to the handling function). I found this interesting post https://stackoverflow.com/questions/4968194/event-keyword-in-js which deals with similar issue and recommendation is to separate the data and JS, but I did not understand it fully.
So maybe lets go to practical use case, I use a serialization to store parts of the web-page in the JS variables and having handlers in DOM is very useful because I can handle everything as a single module and do not worry to re-attach all events after un-serialize. Since the live() function was depreciated in $ it seems the best way to handle this task. And also I would like to avoid changing it and refactoring all legacy code.
So, what's the recommendation for this case, or maybe I just should just drop the old way and invest into listening and binding events on the document level?
'event' is definitely not a keyword, but, indeed, it should be used to refer to Event object when using inline events. See https://stackoverflow.com/questions/11265869/how-does-javascript-recogize-event-object-variables. But this is not considered a good practice currently (this is probably the reason for marking it deprecated in lib.*.d.ts provided by Microsoft)
Anyway, I'm not sure what can be done here on IDE end