My Kind of Stupid

Web development, user interfaces, and other development topics.

onclick is Your Friend

Mark asserts that while using onclick is frowned upon, it's faster and cleaner:

Sure it makes your XHTML a bit longer, but that’s one less selector jQuery has to parse and one less DOMReady function your browser has to kick off.

That's true, but it comes at the cost of maintainability. Putting the jQuery example he provided into an external file makes it easier to maintain by having all the JavaScript in predictable and findable places.

Additionally, that performance hit can be mitigated by using progressive enhancement to give the entire page functionality immediately, then asynchronously loading the JavaScript to enhance it. Asynchronously loading your JS may or may not even be necessary, depending on your JS payload.

Using inline code like this isn't necessary for most people; they just make your code harder to read.

Published at