guest271314 guest271314 - 1 year ago 76
HTML Question

When did using global event handler attributes within html become "considered bad practice"?

Related: Why is using onClick() in HTML a bad practice?. See also Unobtrusive JavaScript.

Unobtrusive JavaScript is a general approach to the use of JavaScript
in web pages. Though the term is not formally defined (emphasis added)


  1. At what point was the determination made that using
    other global event handler attributes within
    was "considered"
    a "bad practice"?

  2. By whom was the determination made that using global event handler attributes within
    was a "bad practice"?

  3. If the user who drafts the
    and includes global event handler attributes within the
    is aware of the event handler
    attributes' presence within the
    , how could this be
    "considered" "bad practice"?

  4. Should a composer of
    not ever include an event handler attribute within

  5. Are there any cases where including event handler attributes within
    would not be "considered" a "bad practice"?

  6. Exactly whose "consideration" of a practice should the composer of the
    defer to? That is, what is the
    reference point for a composer to review whether a particular
    practice is currently "considered" "bad", "not bad" or "good"? Or,
    is there a list which tallies all "considerations" of those that
    "consider" the many possible practices which could be implemented

For clarification, attempting to ascertain exactly when, and by whom, the term or phrase "bad practice" or "considered bad practice" was attached to the usage of global event handlers within
? And what is the official or pseudo-official document or set of documents which list the votes of those that participated in the "consideration" process that leads to a "bad" or "good" designation?

Or, is the term "considered bad practice" primarily opinion-based?

Answer Source

First off, it is considered bad practice by "the community" -- the collective intelligence of average JavaScript developers.

Bad practice, by definition, is not something that's a rule that's decided by anybody, just like you can be bad at naming functions or have too long functions or using global variables all over the place, you can have your JavaScript event handlers assigned in HTML.

There was no poll and no W3C decision made. It's simply a result of many people's experience with them.

The main reasons, in my opinion are:

  • The event handlers need to be global references, so everything that's bad about global variables in general applies to them.
  • It's not very obvious where your global function is being used if you're only looking at the JavaScript code
  • People generally prefer to keep their HTML clean of JavaScript and leaving their event handling completely in JavaScript land.
  • Keeping the event handling assignment in JavaScript makes it so you don't have to change the markup to update your JavaScript code.
  • If you have a lot of them around, it can get pretty repetitive. For example, when assigning the same event handler to every row in a table.
  • You can't do all your event assignment in HTML, so you inevitably will have some in HTML and some in JavaScript land, which makes it feel extra messy.

However, there's a lot of dogma, and generally when there's a new way to do things the sheep herd will generally just run around in the same direction eschewing any other opinions. You'll hear a lot of people saying it's bad without them necessarily knowing why it's bad or knowing how to explain why they're bad.

So, being completely pragmatic about it, if you have a simple application and you just need to assign a single event handler, etc., it's your choice.

So IMO there's nothing wrong with it, and if you and the people you're working with like doing it that way, by all means, go ahead.

But yes, it's considered bad practice by the average JavaScript developer. With no authority.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download