侵入式和非侵入式 javascript 之间的区别
用简单的英语来说,侵入式和非侵入式 javascript 有什么区别。简洁受到赞赏。简短的例子也值得赞赏。
What is the difference between obtrusive and unobtrusive javascript - in plain english. Brevity is appreciated. Short examples are also appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
标记中的 JavaScript 不显眼:
显眼:
显眼:
No javascript in the markup is unobtrusive:
Obtrusive:
Unobtrusive:
我不再认可这一点,因为它在 2011 年有效,但可能在 2018 年及以后不再有效。
关注点分离。 你的 HTML 和 CSS 不会与你的 JS 绑定在一起代码。您的 JS 代码未内联到某些 HTML 元素。您的代码没有一个大函数(或非函数)来处理所有事情。你的函数简短、简洁。
模块化。
当您正确地分离关注点时,就会发生这种情况。例如,您出色的画布动画不需要知道矢量如何工作来绘制盒子。
如果他们没有安装 JavaScript,或者没有运行最新的浏览器,不要破坏体验 - 尽你所能来优雅地降低体验。
当您只需要做一些小事情时,不要构建堆积如山的无用代码。人们通过重新选择 DOM 元素、搞乱语义 HTML、在其中乱扔编号 ID,以及由于不理解文档模型或其他一些技术而发生的其他奇怪的事情,不断地使代码变得复杂——因此他们依赖于“神奇”的抽象层可以将一切速度减慢到垃圾速度,并带来大量的开销。
I don't endorse this anymore as it was valid in 2011 but perhaps not in 2018 and beyond.
Separation of concerns. Your HTML and CSS aren't tied into your JS code. Your JS code isn't inline to some HTML element. Your code doesn't have one big function (or non-function) for everything. You have short, succinct functions.
Modular.
This happens when you correctly separate concerns. Eg, Your awesome canvas animation doesn't need to know how vectors work in order to draw a box.
Don't kill the experience if they don't have JavaScript installed, or aren't running the most recent browsers-- do what you can to gracefully degrade experience.
Don't build mountains of useless code when you only need to do something small. People endlessly complicate their code by re-selecting DOM elements, goofing up semantic HTML and tossing numbered IDs in there, and other strange things that happen because they don't understand the document model or some other bit of technology-- so they rely on "magic" abstraction layers that slow everything down to garbage-speed and bring in mountains of overhead.
如需冗长的解释,请查看有关该主题的维基百科页面。
For a long-winded explanation, checkout the Wikipedia page on the subject.
为了扩展 Mike 的答案:使用 UJS 行为是“稍后”添加的。
UJS 也可能意味着温和的降级(我最喜欢的那种),例如,获取
#info
点击功能的另一种方法,也许是通过提供等效的链接。换句话说,如果没有 JavaScript,或者我正在使用屏幕阅读器等,会发生什么。To expand on Mike's answer: using UJS behavior is added "later".
UJS may also imply gentle degradation (my favorite kind), for example, another means to get to the
#info
click functionality, perhaps by providing an equivalent link. In other words, what happens if there's no JavaScript, or I'm using a screen reader, etc.不引人注目 - “不引人注目、不引人注目、不自信或沉默寡言。”
唐突 - “具有或表现出唐突的倾向,例如将自己或自己的观点强加于他人。”
obtrude - “将(某事)推进或强加给某人,尤其是在没有授权或邀请的情况下”
因此,说到强加某人的观点,在我看来,不引人注目的 JavaScript 最重要的部分是 从用户的角度来看,它不会造成妨碍。也就是说,如果浏览器设置关闭 JavaScript,该网站仍然可以运行。无论 JavaScript 是否打开,使用屏幕阅读器、键盘(不使用鼠标)以及其他辅助工具的人们仍然可以访问该网站。也许(可能)该网站对于此类用户来说不会那么“花哨”,但它仍然可以工作。
如果您从“渐进增强”的角度考虑,您网站的核心功能将适用于每个人,无论他们如何访问它。然后,对于启用了 JavaScript 和 CSS 的用户(大多数用户),您可以使用更多交互元素来增强它。
另一个关键的“不引人注目”因素是“关注点分离”——程序员关心的事情,而不是用户关心的事情,但它可以帮助阻止 JavaScript 方面的事情干扰用户的体验。从程序员的角度来看,避免内联脚本确实会使标记更漂亮且更易于维护。通常,调试不分散在一堆内联事件处理程序中的脚本要容易得多。
unobtrusive - "not obtrusive; inconspicuous, unassertive, or reticent."
obtrusive - "having or showing a disposition to obtrude, as by imposing oneself or one's opinions on others."
obtrude - "to thrust (something) forward or upon a person, especially without warrant or invitation"
So, speaking of imposing one's opinions, in my opinion the most important part of unobtrusive JavaScript is that from the user's point of view it doesn't get in the way. That is, the site will still work if JavaScript is turned off by browser settings. With or without JavaScript turned on the site will still be accessible to people using screen readers, a keyboard and no mouse, and other accessibility tools. Maybe (probably) the site won't be as "fancy" for such users, but it will still work.
If you think in term's of "progressive enhancement" your site's core functionality will work for everybody no matter how they access it. Then for users with JavaScript and CSS enabled (most users) you enhance it with more interactive elements.
The other key "unobtrusive" factor is "separation of concerns" - something programmers care about, not users, but it can help stop the JavaScript side of things from obtruding on the users' experience. From the programmer's point of view avoiding inline script does tend to make the markup a lot prettier and easier to maintain. It's generally a lot easier to debug script that isn't scattered across a bunch of inline event handlers.
即使您不使用 ruby on Rails,这些前几段仍然提供了很好的解释不引人注目的 javascript 的好处。
总结如下:
Even if you don't do ruby on rails, these first few paragraphs still offer a great explanation of the benefits of unobtrusive javascript.
Here's a summary: