JavaScript
js/no-inline-handlers
Attach event handlers from script with addEventListener, not with onclick="..." attributes in HTML.
umiarkowanastatycznefe-javascript
Dlaczego
An onclick="save()" attribute is a string of script inside the markup: it only reaches functions on the global object, mixes behaviour into the HTML, and is blocked by any Content Security Policy that does not allow unsafe-inline, which is the main protection against injected script.
Poprawka
Give the element a hook (id, a class or a data- attribute) and attach the handler from a module: button.addEventListener('click', save). For many similar elements, add one listener to their container and find the target with event.target.closest(). Framework handlers (onClick={...}, @click, (click)) are not inline attributes and are fine.
Przykłady
<!-- bad -->
<button type="button" onclick="save()">Save</button>
<!-- good -->
<button type="button" id="save">Save</button>
<script type="module">document.getElementById('save').addEventListener('click', save)</script>Źródła
Fakty
- Identyfikator
js/no-inline-handlers- Waga
- umiarkowana
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
htmlastro- Uczy jej skill
fe-javascriptJavaScript in the browser- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain js/no-inline-handlers