1

Why should I not use inline JavaScript, such as onclick, onmouseup, or onmousedown? I posted an answer in a question earlier (it got multiple downvotes so I deleted it) and in the comments they said not to use inline JavaScript as it is outdated, yet others such as Angular and React still use inline JavaScript, so why can’t we use it in HTML? Why is it not a good thing to use and why is it outdated?

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • My downvote had nothing to do with the inline event. – epascarello Oct 29 '18 at 23:01
  • @epascarello I know I wasnt saying it did. I really dont care about that. I just want to know why we should not use it. –  Oct 29 '18 at 23:02
  • https://stackoverflow.com/questions/5871640/why-is-using-onclick-in-html-a-bad-practice – epascarello Oct 29 '18 at 23:02
  • https://softwareengineering.stackexchange.com/questions/86589/why-should-i-avoid-inline-scripting – Tomas Crofty Oct 29 '18 at 23:03
  • https://stackoverflow.com/questions/11737873/why-is-inline-event-handler-attributes-a-bad-idea-in-modern-semantic-html – epascarello Oct 29 '18 at 23:03
  • 1
    I would say it's just an effort to keep the different components of the web page separate from each other, so that the JS and the CSS is separate from the markup. I still use inline JS when it's still convenient to do so. – MichaelvE Oct 29 '18 at 23:04
  • I was taught that you keept the three legs of code (html / css / javascript) apart as much as possible so as to present clean code and have it as readable and maintainable as possible. It is one of the things that I HATE about Angular is the amount of inline content - not just the click handlers but ng-style etc which breaks the separation and does my head in. – gavgrif Oct 29 '18 at 23:07
  • thank you @gavgrif I realize that this post is closed as duplicate so you can't post an answer but thank you for your comment. –  Oct 29 '18 at 23:09
  • 1
    Don’t look to Angular for good practices :D (Although the bad parts of Angular don’t have much to do with inline events.) Anyway, another downside of inline event handlers is that they make it impossible to write a restrictive [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP). – Ry- Oct 29 '18 at 23:09
  • thank you @Ry- And thank you to everyone else who took the time to answer this question in the comments as it was closed, yet still answered, so thank you very much. –  Oct 29 '18 at 23:11
  • Dont use it, because the use of `script tags` and external javascript is more reliable – Universal Omega Oct 29 '18 at 23:27

0 Answers0