6 ms·
> Use onclick In Anchors Instead Of javascript: Pseudo-Protocol WHAT? No, you do NOT mix JavaScript into your HTML elements. Total disregard for separation of
by bdg 15y ago
> Use onclick In Anchors Instead Of javascript: Pseudo-Protocol
WHAT?
No, you do NOT mix JavaScript into your HTML elements. Total disregard for separation of concerns, maintainability nightmares abound.
You put script, surprisingly, into the <script> tag.
- joshuahedlund 15y agoAs I understand it, the main point of that section is what to do with the onclick handler vs. what to do with the href attribute. You can follow the suggested practices regardless of whether you put your onclick code inside the element (as in the simple example) or in the script tag (as you rightly recommend).
- firefoxman1 15y agoYeah, the author is basically telling you to use what is now considered a bad practice (onclick) instead of a worse practice such as... href="javascript: dostuff()"
- iamds 15y agowhat is now considered good practice?
- firefoxman1 15y agoEver since the term "unobtrusive javascript" came into play, it's been considered best practice to have all scripts loaded externally, then select the element and attach a listener to it.
- Hexx 15y agoI think this just may be an old page.
- mrpollo 15y agoSurprisingly best practices dont last much on the web
- epochwolf 15y agoWhat's wrong with putting a single, reusable function call in onclick? <a href="/posts/1244/comments/new" onclick="Comment.new(this); return false;">New Comment</a>
- lazerwalker 15y agoIf nothing else, it goes against the author's own rule of never using eval, albeit in this case it's an implied eval rather than explicit call to the eval() function.
- bdg 15y ago1. Pollutes the DOM 2. Violates separation of concerns 3. Complicates maintainability 4. Events are bound directly to elements rather than bubbling and being managed via delegation.
- mayanksinghal 15y agoI don't use use onclicks either but from the source of this page: onclick="return vote(this)"
- jonah 15y agoThis page uses tables too. [edit] my point was to not take this site as an example of best-practices...
- funkah 15y agoYeah, I didn't see a date anywhere, but parts of this advice seem old. That doesn't matter for some things that will never change, but nobody should be writing onclick attributes in 2012. I have found that when you're looking for JS help or references, you should use the most recent available.