- MooTools 1.2 Beginner's Guide
- Jacob Gube Garrick Cheung
- 482字
- 2021-04-01 13:33:35
Writing unobtrusive JavaScript with MooTools
Unobtrusive JavaScript is a set of techniques and principles for writing code that's separated from your web page's structure.
It's best to learn about unobtrusive JavaScript by way of example.
An "obtrusive" JavaScript example
In this example, you'll see JavaScript code that works perfectly fine but doesn't adhere to unobtrusive JavaScript principles. The script contains a function called mouseClick()
that, when triggered, opens up an alert dialog box with the message, You clicked me!.
The HTML markup is a simple unordered list of links. The hyperlinks<a>
are assigned an onclick
attribute which triggers the mouseClick()
function when you click on them.
<html> <head> <script type="text/javascript"> // A simple function that opens an alert dialog box with a message function mouseClick() { alert( 'You clicked me!' ); } </script> </head> <body> <ul id="nav"> <li><a onclick="javascript:mouseClick();" href="#">Home</a></li> <li><a onclick="javascript:mouseClick();" href="#">About</a></li> <li><a onclick="javascript:mouseClick();" href="#">Contact</a></li> </ul> </body> </html>

What's so bad about it?
If the script works as intended, then what's the big deal?
Imagine this scenario: you have a 30 page website and each web page has to have that list of links; if you noticed, the unordered list (<ul
>) tag has an id
attribute of nav
, so let's pretend that this is your site's primary navigation. You would have to go through all 30 pages to add mouseClick()
to the list of links found on each page. This would be very time consuming.
That's a lot of repetition, and you'll be breaking a lot of MooTools' core developers' hearts because they designed the framework with the Don't Repeat Yourself (DRY) principle.
If you have to manually implement that block of code, there's a big chance that you'll commit an error that can result in your web pages rendering incorrectly or behaving unexpectedly.
Say that for some reason, you want to add something else to the onclick
attributes of the links (like calling another function) or remove it entirely. Whenever there's a change to the onclick
attribute, you'll have to go through each page and change them. That's not an ideal situation, especially because by writing unobtrusively, you can sidestep this issue and write a more maintainable code base.
Modern web development best practices encourage separating your web page's content/structure (HTML) from its presentation (CSS) and behavior/functionality (client-side/server-side scripts). By relying on the onclick
attribute, we're intermixing our web page's structure with its behavior (opening a dialog box when the link is clicked).
Separating structure, style, and functionality goes back to maintainability; it allows us to keep things separate so that we can work with each component individually without affecting the other components. This involves taking out all inline event handlers (namely, the onclick
attribute on our<a>
tags).
Adding on event handlers directly into the HTML could also cause memory leaks in Internet Explorer and make adding multiple events of the same type impossible.
- AI繪畫教程:Midjourney關鍵詞靈感手冊
- Visio圖形設計從新手到高手(兼容版·第2版)
- Protel DXP 2004 SP2原理圖與PCB設計(第4版)
- ERP沙盤模擬教程
- AutoCAD快速自學寶典(2018中文版)
- Microsoft BizTalk Server 2010 Patterns
- Drupal Multimedia
- 中文版AutoCAD基礎培訓教程
- Getting Started With Oracle SOA Suite 11g R1 – A Hands/On Tutorial
- 數字創意大師:Alias二維與三維概念設計工作流淺析
- After Effects影視特效立體化教程:After Effects 2021(微課版)
- 中文版Maya 2016實用教程
- Photoshop 2024從入門到精通
- Maya 2020 超級學習手冊
- AutoCAD 2020中文版入門、精通與實戰