翻譯 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庫對跨瀏覽器的支持的取捨,鼓勵大家效仿雅虎瀏覽器分級支持策略方式制定自己的支持矩陣,從收益和支出兩方面平衡分析得出相對理智實用的支持方案。

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