JavaScript & DHTML Cookbook, 2/e
暫譯: JavaScript 與 DHTML 食譜(第二版)

Danny Goodman

  • 出版商: O'Reilly
  • 出版日期: 2007-09-11
  • 定價: $1,488
  • 售價: 2.0$299
  • 語言: 英文
  • 頁數: 606
  • 裝訂: Paperback
  • ISBN: 0596514085
  • ISBN-13: 9780596514082
  • 相關分類: HTMLJavaScript
  • 立即出貨 (庫存 < 3)

買這商品的人也買了...

相關主題

商品描述

Description

In today's Web 2.0 world, JavaScript and Dynamic HTML are at the center of the hot new approach to designing highly interactive pages on the client side. With this environment in mind, the new edition of this book offers bite-sized solutions to very specific scripting problems that web developers commonly face. Each recipe includes a focused piece of code that you can insert right into your application.

Why is JavaScript & DHTML Cookbook so popular? After reading thousands of forum threads over the years, author and scripting pioneer Danny Goodman has compiled a list of problems that frequently vex scripters of various experience levels. For every problem he addresses, Goodman not only offers code, but a discussion of how and why the solution works. Recipes range from simple tasks, such as manipulating strings and validating dates in JavaScript, to entire libraries that demonstrate complex tasks, such as cross-browser positioning of HTML elements, sorting tables, and implementing Ajax features on the client.

Ideal for novices as well as experienced scripters, this book contains more than 150 recipes for:
  • Working with interactive forms and style sheets
  • Presenting user-friendly page navigation
  • Creating dynamic content via Document Object Model scripting
  • Producing visual effects for stationary content
  • Positioning HTML elements
  • Working with XML data in the browser
Recipes in this Cookbook are compatible with the latest W3C standards and browsers, including Internet Explorer 7, Firefox 2, Safari, and Opera 9. Several new recipes provide client-side Ajax solutions, and many recipes from the previous edition have been revised to help you build extensible user interfaces for Web 2.0 applications. If you want to write your own scripts and understand how they work, rather than rely on a commercial web development framework, the JavaScript & DHTML Cookbook is a must.

Table of Contents

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 

      2.5 Formatting Numbers for Text Display 

      2.6 Converting Between Decimal and Hexadecimal Numbers 

      2.7 Generating Pseudorandom Numbers 

      2.8 Calculating Trigonometric Functions 

      2.9 Creating a Date Object 

      2.10 Calculating a Previous or Future Date 

      2.11 Calculating the Number of Days Between Two Dates 

      2.12 Validating a Date 

3. Arrays and Objects

      3.1 Creating a Simple Array 

      3.2 Creating a Multidimensional Array 

      3.3 Converting Between Arrays and Strings 

      3.4 Doing Something with the Items in an Array 

      3.5 Sorting a Simple Array 

      3.6 Combining Arrays 

      3.7 Dividing Arrays 

      3.8 Creating a Custom Object 

      3.9 Simulating a Hash Table for Fast Array Lookup 

      3.10 Doing Something with a Property of an Object 

      3.11 Sorting an Array of Objects 

      3.12 Customizing an Object's Prototype 

      3.13 Converting Arrays and Custom Objects to Strings 

      3.14 Using Objects to Reduce Naming Conflicts 

4. Variables, Functions, and Flow Control

      4.1 Creating a JavaScript Variable 

      4.2 Creating a Named Function 

      4.3 Nesting Named Functions 

      4.4 Creating an Anonymous Function 

      4.5 Delaying a Function Call 

      4.6 Branching Execution Based on Conditions 

      4.7 Handling Script Errors Gracefully 

      4.8 Improving Script Performance 

5. Browser Feature Detection

      5.1 Detecting the Browser Brand 

      5.2 Detecting an Early Browser Version 

      5.3 Detecting the Internet Explorer Version 

      5.4 Detecting the Mozilla Version 

      5.5 Detecting the Safari Version 

      5.6 Detecting the Opera Version 

      5.7 Detecting the Client Operating System 

      5.8 Detecting Object Support 

      5.9 Detecting Object Property and Method Support 

      5.10 Detecting W3C DOM Standard Support 

      5.11 Detecting the Browser Written Language 

      5.12 Detecting Cookie Availability 

      5.13 Defining Browser- or Feature-Specific Links 

      5.14 Testing on Multiple Browser Versions 

6. Managing Browser Windows

      6.1 Living with Browser Window Control Limitations 

      6.2 Setting the Main Window's Size 

      6.3 Positioning the Main Window 

      6.4 Maximizing the Main Window 

      6.5 Creating a New Window 

      6.6 Bringing a Window to the Front 

      6.7 Communicating with a New Window 

      6.8 Communicating Back to the Main Window 

      6.9 Using Internet Explorer Modal/Modeless Windows 

      6.10 Simulating a Cross-Browser Modal Dialog Window 

      6.11 Simulating a Window with Layers 

7. Managing Multiple Frames

      7.1 Creating a Blank Frame in a New Frameset 

      7.2 Changing the Content of One Frame from Another 

      7.3 Changing the Content of Multiple Frames at Once 

      7.4 Replacing a Frameset with a Single Page 

      7.5 Avoiding Being "Framed" by Another Site 

      7.6 Ensuring a Page Loads in Its Frameset 

      7.7 Reading a Frame's Dimensions 

      7.8 Resizing Frames 

      7.9 Setting Frameset Specifications Dynamically 

8. Dynamic Forms

      8.1 Auto-Focusing the First Text Field 

      8.2 Performing Common Text Field Validations 

      8.3 Preventing Form Submission upon Validation Failure 

      8.4 Auto-Focusing an Invalid Text Field Entry 

      8.5 Using a Custom Validation Object 

      8.6 Changing a Form's Action 

      8.7 Blocking Submissions from the Enter Key 

      8.8 Advancing Text Field Focus with the Enter Key 

      8.9 Submitting a Form by an Enter Key Press in Any Text Box 

      8.10 Disabling Form Controls 

      8.11 Hiding and Showing Form Controls 

      8.12 Allowing Only Numbers (or Letters) in a Text Box 

      8.13 Auto-Tabbing for Fixed-Length Text Boxes 

      8.14 Changing select Element Content 

      8.15 Copying Form Data Between Pages 

9. Managing Events

      9.1 Equalizing the IE and W3C Event Models 

      9.2 Initiating a Process After the Page Loads 

      9.3 Appending Multiple Load Event Handlers 

      9.4 Determining the Coordinates of a Click Event 

      9.5 Preventing an Event from Performing Its Default Behavior 

      9.6 Blocking Duplicate Clicks 

      9.7 Determining Which Element Received an Event 

      9.8 Determining Which Mouse Button Was Pressed 

      9.9 Reading Which Character Key Was Typed 

      9.10 Reading Which Noncharacter Key Was Pressed 

      9.11 Determining Which Modifier Keys Were Pressed During an Event 

      9.12 Determining the Element the Cursor Rolled From/To 

      9.13 Synchronizing Sounds to Events 

10. Page Navigation Techniques

      10.1 Loading a New Page or Anchor 

      10.2 Keeping a Page Out of the Browser History 

      10.3 Using a select Element for Navigation 

      10.4 Passing Data Between Pages via Cookies 

      10.5 Passing Data Between Pages via Frames 

      10.6 Passing Data Between Pages via URLs 

      10.7 Creating a Contextual (Right-Click) Menu 

      10.8 Creating Drop-Down Navigation Menus 

      10.9 Providing Navigation Trail Menus 

      10.10 Creating Expandable Menus 

      10.11 Creating Collapsible XML Menus 

11. Managing Style Sheets

      11.1 Assigning Style Sheet Rules to an Element Globally 

      11.2 Assigning Style Sheet Rules to a Subgroup of Elements 

      11.3 Assigning Style Sheet Rules to an Individual Element 

      11.4 Importing External Style Sheets 

      11.5 Importing Browser- or Operating System-Specific Style Sheets 

      11.6 Changing Imported Style Sheets After Loading 

      11.7 Enabling/Disabling Style Sheets 

      11.8 Toggling Between Style Sheets for an Element 

      11.9 Overriding a Style Sheet Rule 

      11.10 Turning Arbitrary Content into a Styled Element 

      11.11 Creating Center-Aligned Body Elements 

      11.12 Reading Effective Style Sheet Property Values 

      11.13 Forcing Recent Browsers into Standards-Compatibility Mode 

12. Visual Effects for Stationary Content

      12.1 Precaching Images 

      12.2 Swapping Images (Rollovers) 

      12.3 Reducing Rollover Image Downloads with JavaScript 

      12.4 Reducing Rollover Image Downloads with CSS 

      12.5 Dynamically Changing Image Sizes 

      12.6 Changing Text Style Properties 

      12.7 Offering Body Text Size Choices to Users 

      12.8 Creating Custom Link Styles 

      12.9 Changing Page Background Colors and Images 

      12.10 Hiding and Showing Elements 

      12.11 Adjusting Element Transparency 

      12.12 Creating Transition Visual Effects 

      12.13 Drawing Charts in the Canvas Element 

13. Positioning HTML Elements

      13.1 Making an Element Positionable in the Document Space 

      13.2 Connecting a Positioned Element to a Body Element 

      13.3 Controlling Positioning via a DHTML JavaScript Library 

      13.4 Deciding Between div and span Containers 

      13.5 Adjusting Positioned Element Stacking Order (Z-order) 

      13.6 Centering an Element on Top of Another Element 

      13.7 Centering an Element in a Window or Frame 

      13.8 Determining the Location of a Nonpositioned Element 

      13.9 Animating Straight-Line Element Paths 

      13.10 Animating Circular Element Paths 

      13.11 Creating a Draggable Element 

      13.12 Scrolling div Content 

      13.13 Creating a Custom Scrollbar 

      13.14 Creating a Slider Control 

14. Creating Dynamic Content

      14.1 Writing Dynamic Content During Page Loading 

      14.2 Creating New Page Content Dynamically 

      14.3 Including External HTML Content 

      14.4 Embedding XML Data 

      14.5 Embedding Data As JavaScript Objects 

      14.6 Transforming XML Data into HTML Tables 

      14.7 Transforming JavaScript Objects into HTML Tables 

      14.8 Converting an XML Node Tree to JavaScript Objects 

      14.9 Creating a New HTML Element 

      14.10 Creating Text Content for a New Element 

      14.11 Creating Mixed Element and Text Nodes 

      14.12 Inserting and Populating an iframe Element 

      14.13 Getting a Reference to an HTML Element Object 

      14.14 Referencing All Elements of the Same Class 

      14.15 Replacing Portions of Body Content 

      14.16 Removing Body Content 

      14.17 Using XMLHttpRequest for a REST Request 

      14.18 Using XMLHttpRequest for a SOAP Call 

      14.19 Sorting Dynamic Tables 

      14.20 Walking the Document Node Tree 

      14.21 Capturing Document Content 

15. Dynamic Content Applications

      15.1 Displaying a Random Aphorism 

      15.2 Converting a User Selection into an Arbitrary Element 

      15.3 Automating the Search-and-Replace of Body Content 

      15.4 Designing a User-Editable Content Page 

      15.5 Creating a Slide Show 

      15.6 Auto-Scrolling the Page 

      15.7 Greeting Users with Their Time of Day 

      15.8 Displaying the Number of Days Before Christmas 

      15.9 Displaying a Countdown Timer 

      15.10 Creating a Calendar Date Picker 

      15.11 Displaying an Animated Progress Bar 

A. Keyboard Event Character Values

B. Keyboard Key Code Values

C. ECMAScript Reserved Keywords

Index

商品描述(中文翻譯)

**描述**

在當今的 Web 2.0 世界中,JavaScript 和動態 HTML 是設計高度互動頁面的新方法的核心。考慮到這一環境,本書的新版本提供了針對網頁開發者常見的特定腳本問題的簡明解決方案。每個食譜都包含一段可以直接插入到應用程式中的專注代碼。

為什麼《JavaScript & DHTML 食譜》如此受歡迎?經過多年閱讀數千個論壇主題,作者和腳本先驅 Danny Goodman 編纂了一份常常困擾各種經驗水平的腳本編寫者的問題清單。對於他所解決的每個問題,Goodman 不僅提供代碼,還討論了解決方案的運作方式和原因。食譜的範圍從簡單的任務,例如在 JavaScript 中操作字串和驗證日期,到整個庫,展示複雜任務,例如 HTML 元素的跨瀏覽器定位、排序表格和在客戶端實現 Ajax 功能。

本書非常適合新手和經驗豐富的腳本編寫者,包含超過 150 個食譜,涵蓋:
- 與互動表單和樣式表的工作
- 提供用戶友好的頁面導航
- 通過文檔物件模型腳本創建動態內容
- 為靜態內容生成視覺效果
- 定位 HTML 元素
- 在瀏覽器中處理 XML 數據

本食譜中的食譜與最新的 W3C 標準和瀏覽器兼容,包括 Internet Explorer 7、Firefox 2、Safari 和 Opera 9。幾個新的食譜提供了客戶端的 Ajax 解決方案,並且許多來自前一版的食譜已被修訂,以幫助您為 Web 2.0 應用程式構建可擴展的用戶界面。如果您想編寫自己的腳本並了解它們的運作方式,而不是依賴商業網頁開發框架,《JavaScript & DHTML 食譜》是必備之選。

**目錄**

前言
1. 字串
1.1 連接(合併)字串
1.2 改善字串處理性能
1.3 訪問子字串
1.4 改變字串大小寫
1.5 測試兩個字串的相等性
1.6 無需正則表達式測試字串包含性
1.7 使用正則表達式測試字串包含性
1.8 搜尋和替換子字串
1.9 使用特殊和轉義字符
1.10 讀取和寫入 Cookie 的字串
1.11 在 Unicode 值和字串字符之間轉換
1.12 編碼和解碼 URL 字串
1.13 編碼和解碼 Base64 字串

2. 數字和日期
2.1 在數字和字串之間轉換
2.2 測試數字的有效性
2.3 測試數值相等性
2.4 四捨五入浮點數
2.5 格式化數字以供文本顯示
2.6 在十進制和十六進制數字之間轉換
2.7 生成偽隨機數字
2.8 計算三角函數
2.9 創建日期物件
2.10 計算過去或未來的日期
2.11 計算兩個日期之間的天數
2.12 驗證日期

3. 陣列和物件
3.1 創建簡單陣列
3.2 創建多維陣列
3.3 在陣列和字串之間轉換
3.4 對陣列中的項目進行操作
3.5 排序簡單陣列
3.6 合併陣列
3.7 分割陣列
3.8 創建自定義物件
3.9 模擬快速陣列查找的哈希表
3.10 對物件的屬性進行操作
3.11 排序物件陣列
3.12 自定義物件的原型
3.13 將陣列和自定義物件轉換為字串
3.14 使用物件減少命名衝突

4. 變數、函數和流程控制
4.1 創建 JavaScript 變數
4.2 創建命名函數
4.3 嵌套命名函數
4.4 創建匿名函數
4.5 延遲函數調用
4.6 根據條件分支執行
4.7 優雅地處理腳本錯誤
4.8 改善腳本性能

5. 瀏覽器功能檢測
5.1 檢測瀏覽器品牌
5.2 檢測早期瀏覽器版本
5.3 檢測 Internet Explorer 版本
5.4 檢測 Mozilla 版本
5.5 檢測 Safari 版本
5.6 檢測 Opera 版本
5.7 檢測客戶端操作系統
5.8 檢測物件支持
5.9 檢測物件屬性和方法支持
5.10 檢測 W3C DOM 標準支持
5.11 檢測瀏覽器書寫語言
5.12 檢測 Cookie 可用性
5.13 定義瀏覽器或功能特定的鏈接
5.14 在多個瀏覽器版本上進行測試

6. 管理瀏覽器窗口
6.1 適應瀏覽器窗口控制限制
6.2 設定主窗口的大小
6.3 定位主窗口
6.4 最大化主窗口
6.5 創建新窗口
6.6 將窗口置於最前
6.7 與新窗口進行通信
6.8 與主窗口進行回傳通信
6.9 使用 Internet Explorer 模態/非模態窗口
6.10 模擬跨瀏覽器模態對話框窗口
6.11 模擬具有層的窗口

7. 管理多個框架
7.1 在新框架集創建空白框架
7.2 從另一個框架更改一個框架的內容
7.3 同時更改多個框架的內容
7.4 用單頁替換框架集
7.5 避免被其他網站「框架」
7.6 確保頁面在其框架集中加載
7.7 讀取框架的尺寸
7.8 調整框架大小
7.9 動態設置框架集規範

8. 動態表單
8.1 自動聚焦第一個文本字段
8.2 執行常見文本字段驗證
8.3 驗證失敗時防止表單提交
8.4 自動聚焦無效文本字段輸入
8.5 使用自定義驗證物件
8.6 更改表單的動作
8.7 阻止 Enter 鍵的提交
8.8 使用 Enter 鍵推進文本字段焦點
8.9 在任何文本框中按 Enter 鍵提交表單
8.10 禁用表單控件
8.11 隱藏和顯示表單控件
8.12 只允許數字(或字母)在文本框中
8.13 固定長度文本框的自動跳轉
8.14 更改選擇元素內容
8.15 在頁面之間複製表單數據

9. 管理事件
9.1 使 IE 和 W3C 事件模型相等
9.2 在頁面加載後啟動過程
9.3 附加多個加載事件處理程序
9.4 確定點擊事件的坐標
9.5 防止事件執行其默認行為
9.6 阻止重複點擊
9.7 確定哪個元素接收了事件
9.8 確定按下了哪個鼠標按鈕
9.9 讀取按下了哪個字符鍵
9.10 讀取按下了哪個非字符鍵
9.11 確定事件期間按下了哪些修飾鍵
9.12 確定光標從/到哪個元素滾動
9.13 將聲音與事件同步

10. 頁面導航技術
10.1 加載新頁面或錨點
10.2 使頁面不進入瀏覽器歷史
10.3 使用選擇元素進行導航
10.4 通過 Cookie 在頁面之間傳遞數據