JavaScript & DHTML Cookbook(第二版)(英文影印版)
基本信息
- 作者: (美)Danny Goodman [作译者介绍]
- 丛书名: 东南大学出版社O'Reilly系列丛书
- 出版社:东南大学出版社
- ISBN:9787564111403
- 上架时间:2008-4-28
- 出版日期:2008 年3月
- 开本:16开
- 页码:582
- 版次:2-1
- 所属分类:
计算机 > 软件与程序设计 > 网络编程 > javascript
编辑推荐
如果你想编写自己的脚本并理解它们的内部机制,而不是仅仅依赖于一个商业化的网络开发框架,这本《JavaScript&DHTML Cookbook》就是必备书籍。
Danny Goodman从20世纪70年代后期就开始为个人计算机和电子消费产品写作。在以后的岁月里,他最受欢迎的书籍——关于HyperCard、AppleScript、JavaScript以及动态HTML——已经涵盖了各种编程环境,它们对于业余爱好者而言易于理解,对于专家而言也仍然足够强劲。他的《Dynamic HTML权威参考》目前已更新至第三版,是0’Reilly的最畅销书籍之一。
内容简介回到顶部↑
在当今web 2.0的世界里,javascript和动态htm1处于热门新方法的中心,用来设计客户端的高度交互页面。考虑到现实情况,新版《javascript&dhtm1 cookbook》为网络开发人员经常碰到的各种非常特殊的脚本问题提供了易于理解的解决方案。每个实例包括一段有针对性的代码,你可以立刻将其嵌入到你的应用程序当中。在经过数年的时间读过几千个论坛的讨论之后,本书作者同时也是脚本编程先锋的danny goodman汇集了一个问题清单,这些问题经常让各种经验程度的脚本语言开发人员都觉得烦恼。对于他提到的每个问题,goodman不仅提供代码,也阐释解决方法如何生效以及为什么可行。书中的实例涵盖各个方面,从简单任务,如使用javascript操作字符串和验证日期,到展示复杂任务的全套程序库,如跨浏览器定位htm1元素、排序表以及在客户端实现ajax功能。
本书包括150多个实例,同时针对新手和有经验的脚本开发人员:
•用交互的表单和样式表工作
•提供用户友好的页面导航
•通过文档对象模型(document object mode1)脚本制作动态内容
•为静态内容添加视觉效果
•在浏览器中使用xm1数据工作
本书对上一版使用的很多实例都进行了修订,帮助你构建可扩展的web 2.0用户界面,并添加了一些新的实例以提供客户端ajax解决方案。本书中的每一个实例都符合最新的w3c标准,兼容各种浏览器,包括internet exp1orer 7、firefox 2、safari和opera 9。
本书包括150多个实例,同时针对新手和有经验的脚本开发人员:
•用交互的表单和样式表工作
•提供用户友好的页面导航
•通过文档对象模型(document object mode1)脚本制作动态内容
•为静态内容添加视觉效果
•在浏览器中使用xm1数据工作
本书对上一版使用的很多实例都进行了修订,帮助你构建可扩展的web 2.0用户界面,并添加了一些新的实例以提供客户端ajax解决方案。本书中的每一个实例都符合最新的w3c标准,兼容各种浏览器,包括internet exp1orer 7、firefox 2、safari和opera 9。
作译者回到顶部↑
本书提供作译者介绍
Danny Goodman从20世纪70年代后期就开始为个人计算机和电子消费产品写作。在以后的岁月里,他最受欢迎的书籍标题——关于HyperCard、AppleScript、JavaScript以及动态HTML——已经涵盖了各种编程环境,它们对于业余爱好者而言易于理解,对于专家而言也仍然足够强劲。他的《Dynamic HTML权威参考》目前已更新至第三版,是O’Reilly的最畅销书籍之一。...
.. << 查看详细
.. << 查看详细
目录回到顶部↑
preface
1.strings
1.1 concatenating (joining) strings
1.2 improving string handling performance
1.3 accessing substrings
1.4 changing string case
1.5 testing equality of two strings
1.6 testing string containment without regular expressions
1.7 testing string containment with regular expressions
1.8 searching and replacing substrings
1.9 using special and escaped characters
1.10 reading and writing strings for cookies
1.11 converting between unicode values and string characters
1.12 encoding and decoding url strings
1.13 encoding and decoding base64 strings
2.numbers and dates
2.1 converting between numbers and strings
2.2 testing a number's validity
2.3 testing numeric equality
2.4 rounding floating-point numbers
1.strings
1.1 concatenating (joining) strings
1.2 improving string handling performance
1.3 accessing substrings
1.4 changing string case
1.5 testing equality of two strings
1.6 testing string containment without regular expressions
1.7 testing string containment with regular expressions
1.8 searching and replacing substrings
1.9 using special and escaped characters
1.10 reading and writing strings for cookies
1.11 converting between unicode values and string characters
1.12 encoding and decoding url strings
1.13 encoding and decoding base64 strings
2.numbers and dates
2.1 converting between numbers and strings
2.2 testing a number's validity
2.3 testing numeric equality
2.4 rounding floating-point numbers
前言回到顶部↑
It may be difficult to imagine that a technology born as recently as 1995 would have had enough of a life cycle to experience a rise and fall in popularity, followed now by an amazing renaissance. Client-side scripting, begun initially with JavaScript embedded in Netscape Navigator 2, has experienced such a roller coaster ride. A number of early incompatibilities among major browsers caused many a content author's head to ache. But we learned to live with it, as a long period of stability in one platform-Internet Explorer 6, in particular--meant that we could use our well-worn compatibility workarounds without cause for concern. Another stabilizing factor was the W3C DOM Level 2 specification, which remained a major target for browser makers not following Microsoft's proprietary ways. Mozilla, Safari, and Opera used the W3C DOM as the model to implement, even if Microsoft didn't seem to be in a hurry to follow suit in all cases. .
Two factors have contributed to the rebirth of interest in JavaScript and Dynamic HTML. The first is the wide proliferation of broadband connections. Implementing large client-side applications in JavaScript can take a bunch of code, all of which must be downloaded to the browser. At dial-up speeds, piling a 50-75 kilobyte script onto a page could seriously degrade perceived performance; at broadband speeds, nobody notices the difference.
But without a doubt, the major attraction these days is the now widespread availability in all mainstream browsers of a technology first implemented by Microsoft: the XMLHttpRequest object. It's a mouthful (leading some to refer to it as, simply, XHR), but it allows background communication between the browser and server so that a script can request incremental data from the server and update only a portion of a page. It is far more efficient than downloading a bunch of data with the page and less visually disruptive than the old submit-and-wait-for-a-new-page process. To help put a label on the type of applications one can build with this technology, the term Asynchronous JavaScript and XML (Ajax) was coined. In truth, Ajax is simply a catchy handle for an existing technology.
Ajax has opened the floodgates for web developers. Perhaps the most popular first implementation was Google Maps, whereby you could drag your way around a map, while scripts and the XMLHttpRequest object in the background downloaded adjacent blocks of the map in anticipation of your dragging your way over there. It was smooth, fast, and a real joy to use. And now, more powerful applications--word processors, spreadsheets, email clients--are being built with JavaScript and DHTML.
JavaScript in the browser was originally designed for small scripts to work on small client-side tasks. It is still used that way quite a bit around the Web. Not every application is a mega DHTML app. Therefore, this collection of recipes still has plenty of small tasks in mind. At the same time, however, many recipes from the first edition have been revised with scripting practices that will serve both the beginner and the more advanced scripter well. Examples prepare you for the eventuality that your scripting skills will grow, perhaps leading to a mega DHTML app in the future. Even so, there are plenty of times when you need an answer to that age-old programming question: "How do I... ?"
About You
Client-side scripting and DHTML are such broad and deep subjects that virtually every reader coming to this book will have different experience levels, expectations, and perhaps, fears. No book could hope to anticipate every possible question from someone wishing to use these technologies in his web pages. Therefore, this book makes some assumptions about readers at various stages of their experience:
·You have at least rudimentary knowledge of client-side JavaScript concepts. You know how to put scripts into a web page--where [script] tags go, as well as how to link an external .js file into the current page. You also know what variables, strings, numbers, Booleans, arrays, and objects are--even if you don't necessarily remember the precise way they're used with the JavaScript language. This book is not a tutorial, but you can learn a lot from reading the introductions to each chapter and the discussions following each solution.
·You may be a casual scripter, who wants to put a bit of intelligence into a web page for some project or other. You don't use the language or object model every day, so you need a refresher about even some simple things, such as the correct syntax for creating an array or preloading images for fast image rollover effects.
·While surfing the Web, you may have encountered some scripted DHTML effect that you'd like to implement or adapt for your own pages, but either you can't decipher the code you see or you want to "roll your own" version to avoid copyright problems with the code's original owner. If the effect or technique you've seen is fairly popular, this cookbook probably has a recipe for it. You can use these recipes as they are or modify them to fit your designs. There are no royalties or copyrights to worry about, as long as you don't offer these recipes to others as part of a collection of scripts. Of course, if you wish to acknowledge this book in your source code comments, that would be great!
·You may be an experienced web developer who has probed gingerly, if at all, into client-side scripting. The horror stories of yore about browser incompatibilities have kept your focus entirely on server-side programming. But now that so many mainstream sites are using client-side scripting to improve the user experience, you are ready to take another look at what is out there.
·At the far end of the spectrum, you may be an experienced client-side DHTML developer in search of new ideas and techniques. For instance, you may have developed exclusively for the Internet Explorer browser on the Windows platform, but you wish to gravit.ate toward standards-compatible syntax for future coding. ..
Virtually every reader will find that some recipes in this book are too simple and others are too complex for their experience level. I hope the more difficult ones challenge you to learn more and improve your skills. Even if you think you know it all, be sure to check the discussions of the easier recipes for tips and insights that may be new to you.
About the Recipes It's helpful for a reader to know upfront what biases an author has on the book's subject. To carry the cookbook metaphor too far, just as a culinary chef has identifiable procedures and seasonings, so do I format my code in a particular way and employ programming styles that I have adopted and updated over the years.
More important than scripting style, however, are the implementation threads that weave their way throughout the code examples. Because these examples may serve as models for your own development, they are written for maximum clarity to make it easy (I hope) for you to follow the execution logic. Names assigned to variables, functions, objects, and the like are meant to convey their purpose within the context of the example. One of the goals of coding is that the operation of a program should be self-evident to someone else reading the code, even if that "someone else" is the programmer who revisits the code six months later to fix a bug or add a feature. There's no sense in being cryptically clever if no one can understand what you mean by assigning some value to a variable named x.
This book unabashedly favors the W3C DOM way of addressing document objects. You can use this format to reference element objects in browsers starting with Microsoft Internet Explorer 5 and the other mainstream browsers addressed in this edition (Mozilla-based browsers, Safari, and Opera 7 or later), which means that the vast majority of browsers in use today support this standard. Where IE (including IE 7) does not support the standard (as in handling events), all recipes here include efficient cross-browser implementations. You won't find too much in the way of IEonly solutions, especially if they would cover only the Windows version of IE. The long period of browser stability we have enjoyed since the first edition means that visitors to public sites almost never use what are now antique browsers--IE prior to 5.5 and Netscape Navigator 4 or earlier. All recipes are optimized for the current browsers, but they also try to prevent hassles for anyone driving by in her steam-powered browser.
One credo dominates the recipes throughout this book: scripting must add value to static content on the page. Don't look to this book for scripts that cycle background colors to nauseate visitors or make elements bounce around the page while singing "Happy Birthday." You may be able to figure out how to do those horrible things from what you learn in this book, but that's your business. The examples here, while perhaps conservative, are intended to solve real-world problems that scripters and developers face in professional-quality applications.
The scripting techniques and syntax you see throughout this book are designed for maximum forward compatibility. It's difficult to predict the future of any technology, but the W3C DOM and ECMAScript standards, as implemented in today's latest browsers, are the most stable platforms on which to build client-side applications since client-side scripting began. With a bit of code added here and there to degrade gracefully in older browsers, your applications should be running fine well into the future.
What's in This Book
The first four chapters focus on fundamental JavaScript topics. In Chapter 1, Strings, you will see the difference between a string value and a string object. Regular expressions play a big role in string parsing for these recipes. You will also see a reusable library for reading and writing string data to cookies. Chapter 2, Numbers and Dates, includes recipes for handling number formatting and conversions, as well as date calculations that get used in later recipes. Perhaps the most important core JavaScript language chapter is Chapter 3, Arrays and Objects. Recipes in this chapter provide the keys to one- and multidimensional array creation, array sorting, object creation, hash table simulation, and exploration of the prototype inheritance powers of objects. You also see how creating custom objects for your libraries can reduce potential naming conflicts as projects grow. Chapter 4, Variables, Functions, and Flow Control, includes a recipe for improving overall script performance. .
Two factors have contributed to the rebirth of interest in JavaScript and Dynamic HTML. The first is the wide proliferation of broadband connections. Implementing large client-side applications in JavaScript can take a bunch of code, all of which must be downloaded to the browser. At dial-up speeds, piling a 50-75 kilobyte script onto a page could seriously degrade perceived performance; at broadband speeds, nobody notices the difference.
But without a doubt, the major attraction these days is the now widespread availability in all mainstream browsers of a technology first implemented by Microsoft: the XMLHttpRequest object. It's a mouthful (leading some to refer to it as, simply, XHR), but it allows background communication between the browser and server so that a script can request incremental data from the server and update only a portion of a page. It is far more efficient than downloading a bunch of data with the page and less visually disruptive than the old submit-and-wait-for-a-new-page process. To help put a label on the type of applications one can build with this technology, the term Asynchronous JavaScript and XML (Ajax) was coined. In truth, Ajax is simply a catchy handle for an existing technology.
Ajax has opened the floodgates for web developers. Perhaps the most popular first implementation was Google Maps, whereby you could drag your way around a map, while scripts and the XMLHttpRequest object in the background downloaded adjacent blocks of the map in anticipation of your dragging your way over there. It was smooth, fast, and a real joy to use. And now, more powerful applications--word processors, spreadsheets, email clients--are being built with JavaScript and DHTML.
JavaScript in the browser was originally designed for small scripts to work on small client-side tasks. It is still used that way quite a bit around the Web. Not every application is a mega DHTML app. Therefore, this collection of recipes still has plenty of small tasks in mind. At the same time, however, many recipes from the first edition have been revised with scripting practices that will serve both the beginner and the more advanced scripter well. Examples prepare you for the eventuality that your scripting skills will grow, perhaps leading to a mega DHTML app in the future. Even so, there are plenty of times when you need an answer to that age-old programming question: "How do I... ?"
About You
Client-side scripting and DHTML are such broad and deep subjects that virtually every reader coming to this book will have different experience levels, expectations, and perhaps, fears. No book could hope to anticipate every possible question from someone wishing to use these technologies in his web pages. Therefore, this book makes some assumptions about readers at various stages of their experience:
·You have at least rudimentary knowledge of client-side JavaScript concepts. You know how to put scripts into a web page--where [script] tags go, as well as how to link an external .js file into the current page. You also know what variables, strings, numbers, Booleans, arrays, and objects are--even if you don't necessarily remember the precise way they're used with the JavaScript language. This book is not a tutorial, but you can learn a lot from reading the introductions to each chapter and the discussions following each solution.
·You may be a casual scripter, who wants to put a bit of intelligence into a web page for some project or other. You don't use the language or object model every day, so you need a refresher about even some simple things, such as the correct syntax for creating an array or preloading images for fast image rollover effects.
·While surfing the Web, you may have encountered some scripted DHTML effect that you'd like to implement or adapt for your own pages, but either you can't decipher the code you see or you want to "roll your own" version to avoid copyright problems with the code's original owner. If the effect or technique you've seen is fairly popular, this cookbook probably has a recipe for it. You can use these recipes as they are or modify them to fit your designs. There are no royalties or copyrights to worry about, as long as you don't offer these recipes to others as part of a collection of scripts. Of course, if you wish to acknowledge this book in your source code comments, that would be great!
·You may be an experienced web developer who has probed gingerly, if at all, into client-side scripting. The horror stories of yore about browser incompatibilities have kept your focus entirely on server-side programming. But now that so many mainstream sites are using client-side scripting to improve the user experience, you are ready to take another look at what is out there.
·At the far end of the spectrum, you may be an experienced client-side DHTML developer in search of new ideas and techniques. For instance, you may have developed exclusively for the Internet Explorer browser on the Windows platform, but you wish to gravit.ate toward standards-compatible syntax for future coding. ..
Virtually every reader will find that some recipes in this book are too simple and others are too complex for their experience level. I hope the more difficult ones challenge you to learn more and improve your skills. Even if you think you know it all, be sure to check the discussions of the easier recipes for tips and insights that may be new to you.
About the Recipes It's helpful for a reader to know upfront what biases an author has on the book's subject. To carry the cookbook metaphor too far, just as a culinary chef has identifiable procedures and seasonings, so do I format my code in a particular way and employ programming styles that I have adopted and updated over the years.
More important than scripting style, however, are the implementation threads that weave their way throughout the code examples. Because these examples may serve as models for your own development, they are written for maximum clarity to make it easy (I hope) for you to follow the execution logic. Names assigned to variables, functions, objects, and the like are meant to convey their purpose within the context of the example. One of the goals of coding is that the operation of a program should be self-evident to someone else reading the code, even if that "someone else" is the programmer who revisits the code six months later to fix a bug or add a feature. There's no sense in being cryptically clever if no one can understand what you mean by assigning some value to a variable named x.
This book unabashedly favors the W3C DOM way of addressing document objects. You can use this format to reference element objects in browsers starting with Microsoft Internet Explorer 5 and the other mainstream browsers addressed in this edition (Mozilla-based browsers, Safari, and Opera 7 or later), which means that the vast majority of browsers in use today support this standard. Where IE (including IE 7) does not support the standard (as in handling events), all recipes here include efficient cross-browser implementations. You won't find too much in the way of IEonly solutions, especially if they would cover only the Windows version of IE. The long period of browser stability we have enjoyed since the first edition means that visitors to public sites almost never use what are now antique browsers--IE prior to 5.5 and Netscape Navigator 4 or earlier. All recipes are optimized for the current browsers, but they also try to prevent hassles for anyone driving by in her steam-powered browser.
One credo dominates the recipes throughout this book: scripting must add value to static content on the page. Don't look to this book for scripts that cycle background colors to nauseate visitors or make elements bounce around the page while singing "Happy Birthday." You may be able to figure out how to do those horrible things from what you learn in this book, but that's your business. The examples here, while perhaps conservative, are intended to solve real-world problems that scripters and developers face in professional-quality applications.
The scripting techniques and syntax you see throughout this book are designed for maximum forward compatibility. It's difficult to predict the future of any technology, but the W3C DOM and ECMAScript standards, as implemented in today's latest browsers, are the most stable platforms on which to build client-side applications since client-side scripting began. With a bit of code added here and there to degrade gracefully in older browsers, your applications should be running fine well into the future.
What's in This Book
The first four chapters focus on fundamental JavaScript topics. In Chapter 1, Strings, you will see the difference between a string value and a string object. Regular expressions play a big role in string parsing for these recipes. You will also see a reusable library for reading and writing string data to cookies. Chapter 2, Numbers and Dates, includes recipes for handling number formatting and conversions, as well as date calculations that get used in later recipes. Perhaps the most important core JavaScript language chapter is Chapter 3, Arrays and Objects. Recipes in this chapter provide the keys to one- and multidimensional array creation, array sorting, object creation, hash table simulation, and exploration of the prototype inheritance powers of objects. You also see how creating custom objects for your libraries can reduce potential naming conflicts as projects grow. Chapter 4, Variables, Functions, and Flow Control, includes a recipe for improving overall script performance. .
【插图】







点击看大图


加载中...


