I have heard many times that using JavaScript events, such as onClick()
, in HTML is a bad practice, because it's not good for semantics. I would like to know what the downsides are and how to fix the following code?
<a href="#" onclick="popup('/map/', 300, 300, 'map'); return false;">link</a>
If you are using jQuery then:
HTML:
JS:
This has the benefit of still working without JS, or if the user middle clicks the link.
It also means that I could handle generic popups by rewriting again to:
HTML:
JS:
This would let you add a popup to any link by just giving it the popup class.
This idea could be extended even further like so:
HTML:
JS:
I can now use the same bit of code for lots of popups on my whole site without having to write loads of onclick stuff! Yay for reusability!
It also means that if later on I decide that popups are bad practice, (which they are!) and that I want to replace them with a lightbox style modal window, I can change:
to
and all my popups on my whole site are now working totally differently. I could even do feature detection to decide what to do on a popup, or store a users preference to allow them or not. With the inline onclick, this requires a huge copy and pasting effort.
It's not good for several reasons:
eval
The simplest thing would be to add a
name
attribute to your<a>
element, then you could do:although modern best practise would be to use an
id
instead of a name, and useaddEventListener()
instead of usingonclick
since that allows you to bind multiple functions to a single event.It's a
newparadigm called "Unobtrusive JavaScript". The current "web standard" says to separate functionality and presentation.It's not really a "bad practice", it's just that most new standards want you to use event listeners instead of in-lining JavaScript.
Also, this may just be a personal thing, but I think it's much easier to read when you use event listeners, especially if you have more than 1 JavaScript statement you want to run.
With very large JavaScript applications, programmers are using more encapsulation of code to avoid polluting the global scope. And to make a function available to the onClick action in an HTML element, it has to be in the global scope.
You may have seen JS files that look like this...
These are Immediately Invoked Function Expressions (IIFEs) and any function declared within them will only exist within their internal scope.
If you declare
function doSomething(){}
within an IIFE, then makedoSomething()
an element's onClick action in your HTML page, you'll get an error.If, on the other hand, you create an eventListener for that element within that IIFE and call
doSomething()
when the listener detects a click event, you're good because the listener anddoSomething()
share the IIFE's scope.For little web apps with a minimal amount of code, it doesn't matter. But if you aspire to write large, maintainable codebases,
onclick=""
is a habit that you should work to avoid.Your question will trigger discussion I suppose. The general idea is that it's good to separate behavior and structure. Furthermore, afaik, an inline click handler has to be
eval
led to 'become' a real javascript function. And it's pretty old fashioned, allbeit that that's a pretty shaky argument. Ah, well, read some about it @quirksmode.orgperformance and efficiency.