翻译 Secrets of the JavaScript Ninja 边译边学(4)

Secrets of the JavaScript Ninja 边译边学(4)

1.3 Cross-browser considerations
1.3跨浏览器注意事项

Perfecting our JavaScript programming skills will get us far, but when developing browser-based JavaScript applications sooner, rather than later, we’re going to run face first into The Browsers and their maddening issues and inconsistencies.

完善JavaScript编程技能会使我们受益匪浅,但在开发基于JavaScript的浏览器应用程序时,我们很快将直接面对浏览器及其令人发狂的问题和不一致性。

  In a perfect world, all browsers would be bug-free and support Web Standards in a consistent fashion, but we all know that we most certainly do not live in that world.

  在一个完美的世界里,所有的浏览器都将是无缺陷的,并且以一致的方式支持Web标准,但是我们都知道,我们肯定不生活在那个世界里。

  The quality of browsers has improved greatly as of late, but it’s a given that they all still have some bugs, missing APIs, and specific quirks that we’ll need to deal with. Developing a comprehensive strategy for tackling these browser issues, and becoming intimately familiar with their differences and quirks, is just as important, if not more so, than proficiency in JavaScript itself.

  最近浏览器的质量有了很大的提高,但仍然有许多问题等待我们去解决,包括一些bug,缺失的API以及浏览器一些奇怪的特性。形成一套解决这些浏览器问题的综合战略,并逐渐熟悉它们的区别和特性,已经变得和精通JavaScript本身一样甚至更加重要。

  When writing browser applications, or JavaScript libraries to be used in them, picking and choosing which browsers to support is an important consideration. We’d like to support them all, but development and testing resources dictates otherwise. So how do we decide which to support, and to what level?

  当编写浏览器应用程序,或其使用的JavaScript类库时,选择要支持的浏览器是一个重要的考虑因素。我们当然希望支持所有的浏览器,但是开发和测试资源却不允许我们这样做。所以我们该如何选择支持哪个浏览器,支持到什么程度呢?

  Throughout this book, an approach that we will employ is one that we’ll borrow from Yahoo! that they call Graded Browser Support.

  纵观本书,我们将采用的方法是从Yahoo借鉴而来的,我们称之为分级浏览器支持

  This technique, in which the level of browser support is graded as one of A, C, or X, is described at http://developer.yahoo.com/yui/articles/gbs, and defines the three level of support as:

  在这种技术中,浏览器支持级别被分为A、C或X级,详细描述见http://developer.yahoo.com/yui/articles/gbs,这三个浏览器支持级别定义如下:

  • A: Modern browsers that take advantage of the power capabilities of web standards. These browsers get full support with advanced functionality and visuals.

  • C: Older browsers that are either outdated or hardly used. These browsers receive minimal support; usually limited to HTML and CSS with no scripting, and bare-bones visuals.

  • X: Unknown or fringe browsers. Somewhat counter-intuitively, rather than being unsupported, these browsers are given the benefit of the doubt, and assumed to be as capable as A-grade browsers. Once the level of capability can be concretely ascertained, these browsers can be assigned to A or C grade.

  • A:利用Web标准的强大功能的现代浏览器。这些浏览器完全支持高级功能和视觉效果.

  • C:过时或很少使用的旧浏览器。这些浏览器得到的支持很少;通常仅限于HTML和不带脚本的CSS,以及简单的视觉效果。

  • X:未知或边缘浏览器。这些浏览器在某种程度上与直觉相反,相比不被支持,它们暂且获得了支持,而且假定具备和A级别浏览器一样的功能。一旦可以具体确定功能级别,就可以将这些浏览器正式定义成A或C级。

  As of early 2011, the Yahoo! Graded Browser Support matrix was as shown in Table 1.1. Any ungraded browser/platform combination, or unlisted browser, is assigned a grade of X.

  截止2011年初,雅虎分级浏览器支持矩阵如下表1.1所示。所有未分级的浏览器/平台组合,或者没有列出的浏览器都被指定为X级。

表1.1 2011年初,雅虎分级浏览器支持矩阵

  Note that this is the support chart as defined by Yahoo! for YUI 2 and YUI 3 – it is not a recommendation on what we, in this book, or you, in your own projects, should support. But by using this approach to come up with our own support matrix, we can determine the balance between coverage and pragmatism to come up with the optimal set of browsers and platforms that we should support.

  注意这是由雅虎定义的支持表,用于YUI 2和YUI 3(YUI 库,全称Yahoo! UI Library。是一组工具和控件),而不是在本书中我们推荐的,或者你自己项目中使用的分级策略。但是通过使用这种方法可以绘制我们自己的支持矩阵,方便我们在覆盖范围和实用主义之间找到一个平衡点,提出最佳的浏览器和平台支持集合。

  It’s impractical to develop against a large number of platform/browser combinations, so we must weigh the cost versus benefit of supporting the various browsers, and create our own resulting support matrix.

  针对大量平台/浏览器组合进行开发是不切实际的,因此我们必须权衡支持各种浏览器的成本与收益,然后创建我们自己的最终支持矩阵。

  This analysis must take in account multiple considerations, the primary of which are:

  该分析必须考虑多种因素,其中主要因素有:

  • The market share of the browser

  • The amount of effort necessary to support the browser

  • 该浏览器的市场份额

  • 支持该浏览器需要付出的成本

  Figure 1.2 shows a sample chart that represents your authors’ personal choices when developing for some browsers (not all browsers included for brevity) based upon March 2011 market share:

  图表1.2展示了一个示例图,表达了本书作者为一些浏览器(为简洁没有显示所有的浏览器)开发时的个人选择,该图基于2011年的市场份额:

图表1.2对比分析支持不同浏览器的收益和支出能告诉我们应该在哪个浏览器上付出努力

  Charting the benefit versus cost in this manner shows us at a glance where we should put our effort to get the most “bang for the buck”. Things that jump out of this chart:

以这种方式绘制效益与成本的图表,可以让我们一目了然地看到我们应该把精力放在哪里,来获得最大的"资金回报"。从图中可以看出:

  • Even though it’s relatively a lot more effort to support Internet Explorer 7 and later than the standards-compliant browsers, it’s large market share makes the extra effort worthwhile.

  • Supporting Firefox and Chrome is a no-brainer since that have large market share and are easy to support.

  • Even though Safari has a relatively low market share, it still deserves support, as its standard-compliant nature makes its cost small.

  • Opera, though no more effort than Safari, loses out because of its minuscule market share.

  • Nothing really need be said about IE 6

  • 尽管支持IE 7浏览器所需要的工作量远远超出标准浏览器,但是它巨大的市场份额使得额外的工作付出是值得的。

  • 支持火狐和Chrome是很容易做的决定,因为它们有很大的市场份额并且不需要付出额外精力。

  • 尽管Safari所占的市场份额相对较低,但它仍然值得支持,因为它本身的标准兼容性使得开发支持它的库付出很少。

  • 尽管支持Opera所需付出成本低于Safari,但由于其极微小的市场份额使其被淘汰。

  • 至于IE6没什么可说的,答案显而易见。

  Of course, nothing is ever quite so cut-and-dried. It might be safe to say that benefit is more important than cost; it ultimately comes down to the choices of those in the decision-making process, taking into account factors such as the skill of the developers, the needs of the market, and other business concerns. But quantifying the costs versus benefits is a good starting point for making these important support decisions.

  当然,实际情况比这复杂得多。可以肯定地说,好处比成本更重要;它最终取决于结合了多项因素的决策制定过程的选择,这些因素包括开发人员的水平,市场需求和其他商务考虑因素。但是量化成本和收益是做出这些重要支持决策的一个很好的起点。

  Minimizing the cost of cross-browser development is significantly affected by the skill and
experience of the developers, and this book is intended to boost your skill level, so let’s get
to it by looking at best practices as a start.

  将跨浏览器开发的成本降至最低在很大程度上会受到开发人员的技能和经验影响,这本书旨在提高你的技能水平,所以让我们以最佳实践为起点。

总结
本节主要讲js库对跨浏览器的支持的取舍,鼓励大家效仿雅虎浏览器分级支持策略方式制定自己的支持矩阵,从收益和支出两方面平衡分析得出相对理智实用的支持方案。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章