Online Documentation Server
 ÏÎÈÑÊ
ods.com.ua Web
 ÊÀÒÅÃÎÐÈÈ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ÏÎÄÏÈÑÊÀ

 Î ÊÎÏÈÐÀÉÒÀÕ
Âñÿ ïðåäîñòàâëåííàÿ íà ýòîì ñåðâåðå èíôîðìàöèÿ ñîáðàíà íàìè èç ðàçíûõ èñòî÷íèêîâ. Åñëè Âàì êàæåòñÿ, ÷òî ïóáëèêàöèÿ êàêèõ-òî äîêóìåíòîâ íàðóøàåò ÷üè-ëèáî àâòîðñêèå ïðàâà, ñîîáùèòå íàì îá ýòîì.




Previous Table of Contents Next

Chapter 2
Reading This Book

How to Learn JavaScript

This chapter provides you with a detailed outline on how to make the most of this book. Whether you are composing your very first “Hello world” script or looking for an answer to a specific problem, this chapter will guide you through the shortest path to your goal.

Prerequisites

Most of the topics in this book require some knowledge of HTML. Since JavaScript is both based on and enhances HTML, there is no point in learning JavaScript before first creating your very own Web page. Although we explain the more difficult and advanced HTML tags, knowledge of advanced HTML is not a prerequisite.

A Programming Refresher

This book starts with a crash programming course using the JavaScript language. It may serve either as a refresher for experienced programmers or a basic course for nonprogrammers. Chapter 5 discusses data types, variables, identifiers, etc., while Chapter 7 describes JavaScript-supported operators and expressions. Conditional statements are covered in Chapter 8, functions and parameters in Chapter 9. Chapter 13 explains how to use arrays, and Chapter 15 shows JavaScript’s portfolio of mathematical features.

How to Read This Book

This book includes material for advanced programmers as well. Even if you are a novice programmer, you should have no problem with the first ten chapters, including the comprehensive example in Chapter 10. The most effective way to learn a language is by examples, and you have plenty of those in this book. You should carefully read the example scripts and make sure you understand every line. Naturally, the examples at the end of the book are more complex than those at the beginning, so pace yourself accordingly.

If you are an experienced JavaScript scripter, it is suggested you skim through the beginning of the book until you encounter advanced material you are not fully comfortable with. Once you have digested the new material, skim to the next batch and so on. There are more than fifteen chapters of advanced material, so you’ll find answers to most of your current problems.

The book can also be used as a reference resource. The index at the end of the book and the table of contents at the beginning are to help you find the material you are looking for.

The Structure of This Book

It’s important to understand how the book is structured so you can plan your learning experience in the best possible way. The Table of Contents lists the chapters by name. This section provides you with a brief description of each.

  • Chapter 3 covers Web page authoring tools. It outlines the main advantages and disadvantages of existing tools and provides you some guidelines on which tool to use for each specific task. This chapter also covers the basics on embedding JavaScript in an HTML document and hiding JavaScript code from old browsers that do not support JavaScript. The most exciting feature of this chapter is the famous “Hello World,” or “Hello net,” example script. As this chapter explains how to run a JavaScript script, understanding it is very important for new JavaScript scripters. You also learn in Chapter 3 why you might not be able to print JavaScript output generated with the document.write() method.
  • Chapter 4 describes how an HTML file should be laid out and how JavaScript scripts should be included in it. The chapter talks about the HEAD portion and the BODY portion of an HTML document and how scripts behave if posted in each of these sections. Chapter 4 stresses the difference between deferred scripts and immediate ones and when it is better to choose one over the other. While helping people all over the world debug their scripts, we found misplacement to be a frequent culprit. After reading through Chapter 4, you should know how to define functions and invoke them via a deferred script. If you have no programming background, reading this chapter is essential for understanding more advanced material later in the book.
  • Chapter 5 is an introduction to programming with JavaScript. Since it covers the most important elements of the language, you should carefully read this chapter. It explains the differences between JavaScript and other languages resulting from the fact that JavaScript is loosely typed. The chapter discusses local and global variable declarations, data types, and literals. Each type of literal is explained separately and in depth. A list of escape sequences for special characters is also given. JavaScript entities, a relatively new JavaScript feature which enables the use of JavaScript values in HTML attributes, are introduced.
  • Chapter 6 introduces the object-oriented programming concepts such as objects, properties, and methods. If you haven’t been exposed to OOP, you’ll find this chapter very helpful, as it overviews both general OO concepts as well as JavaScript’s OO specific ones. This chapter also shows how to reference properties and methods using the “dot” syntax as well as the array convention. You may have heard about the OOP paradigm, but it is important for you to know exactly what objects are, how to access their elements, and so forth. Since the chapter explains the difference between OOP and OBP (object-based programming), some parts of this chapter are recommended even for the most advanced object-oriented programmers (C++, Objective C, Java, etc.). JavaScript is object based, as opposed to object oriented, so classes are not supported. Chapter 6 also covers the different types of objects supported by JavaScript.
  • Chapter 7 is a complete reference of all operators featured by JavaScript. Most operators in this chapter are borrowed from C and Perl, but some are not. Since expressions are tightly related to operators, an in-depth discussion of expressions and evaluations in JavaScript is given. We have even uncovered an unpublicized evaluation tool, hidden in all versions of Navigator.
  • Chapter 8 starts off by explaining dialog boxes, including alert(), confirm(), and prompt(). Although these methods are not related to the general topic of the chapter, they are required for demonstrations presented in the chapter. The chapter deals with control structures, from conditional statements to looping through properties. Because JavaScript does not support several known structures from other languages, we provide workarounds that use native JavaScript statements instead. For example, Chapter 8 shows how to use nested conditional statements in place of the missing switch statement, and how to use variables and conditional statements to escape complex situations, thus avoiding the goto statement which is not part of JavaScript’s vocabulary.
  • Chapter 9 presents a concept that may be unfamiliar to some of you—variable scoping. A thorough understanding of this topic is extremely important, because it is impossible to debug a script without knowing where each variable is valid. The chapter introduces some other difficult concepts, such as recursion and how to use variables in a recursive function. It shows the best way to organize your code with functions. You should not skip this chapter, because it is also an introduction to debugging. Chapter 9 formally discusses functions, including arguments, parameters, and returning values. If you are familiar with other languages such as C++, you should only need to skim through this chapter, familiarizing yourself with JavaScript-specific syntax.
  • Chapter 10 is a good point for a break and assessment of what you have learned. In this chapter you learn to combine and implement what you have learned in the previous chapters. Study carefully the two comprehensive scripts, which are explained in great detail. The first example interacts with the user and asks him or her for his or her height, weight, and age. The script then returns the user’s percentile in the population along with some other neat observations. The second script accepts a number in a given base and converts it to a different one. The script also creates a base-conversion table, demonstrating how to generate HTML output using JavaScript only.
  • Chapter 11 presents events and event handlers. Handling events is the cornerstone of user interaction. JavaScript is mostly driven by events that occur on a Web page. An event is a specific action that the user performs, such as clicking a button, loading a page, and so on. All events are user initiated, so you can use event handlers to respond to various actions performed by the user. Chapter 11 covers events and event handlers in general, and several specific event handlers such as onLoad, onUnload, and onError.
  • Chapter 12 discusses objects, properties, and methods. It explains how to create instances of a given object and how to use an instance. In this chapter you also learn how to create constructor functions, so you are not limited to JavaScript’s object model. Chapter 12 is important because JavaScript programming is object based and thus requires a good understanding of objects. This chapter also shows how to debug an object by looping through its properties with a recursive algorithm. By nature, objects can be combined to create complex, nested structures, which are also explained in Chapter 12. This chapter also covers object prototypes by which you can add properties and methods to existing objects, even those that are built into the language.
  • Chapter 13 comes back to the basic data structure of every language—arrays. The idea of arrays is simple and known to most of you, but pay attention to how we present two-dimensional arrays, which are not supported by JavaScript. Unlike other languages, arrays in JavaScript are objects, not explicit data types. Chapter 13 introduces array methods which enable advanced array manipulation. In addition, we developed several scripts that emulate all array function supported by Perl.
  • Chapter 14 is mostly dedicated to the Date object. You can find detailed explanations on creating instances of this object and on using each of its methods and properties. Several examples are included to demonstrate how much more impressive scripts become when using the Date object. Chapter 14 deals with time-related methods of other objects.
  • Chapter 15 brings you back to familiar turf, mathematics. If you have forgotten advanced math, then this chapter is a good refresher. The content, of course, is the mathematical functions that JavaScript provides you for accomplishing various tasks on your Web page. In this chapter we discuss the Math object, its methods (mathematical functions), and its properties (mathematical constants). A graph-plotting script is included as an example of using the Math object.
  • Chapter 16 deals with strings, a very popular literal and data type in JavaScript. Web authoring involves a great amount of string manipulation of user input, document URLs, etc., so rarely will you encounter a script that does not handle strings. This chapter is probably one of the most important ones in the book, because several chapters and many scripts in the book are based on its contents. Even if you are an experienced Perl programmer, you will be surprised to find that JavaScript features more than enough string-related functions and methods.
  • Chapter 17 discusses browser objects and the browser object hierarchy. Because there are many nested browser objects, this chapter focuses on the objects at the highest level. Chapter 17 is mostly theory, and thus does not deal much with JavaScript. It explains the relationship between a Web page’s HTML content and the resulting browser objects.
  • Chapter 18 talks about the thin bar at the bottom of the browser window—the status bar. It may not seem vital, but the status bar has several important uses worth devoting a chapter to. This chapter includes many examples, from scripts that display short messages associated with specific links to special banners. We have developed some unique banners explicitly for this book, including the famous T-banner and R-banner. It is not a surprise then, that a three-page article in Boardwatch Magazine was dedicated to these banners.
  • Chapter 19 deals with the relationships between JavaScript programming and URLs. The chapter first explains the different portions of the URL specification, and then describes the common protocols of the Internet. The location object and its properties and methods are covered. The chapter answers almost every question related to URLs.
  • Chapter 20 discusses JavaScript’s history object. It explains how you can return the user to any entry in his or her history list. This short chapter also presents several security issues related to the history list and the history object in JavaScript. Among these topics you can find information on how to reload a page in two different ways, each with its own advantages and disadvantages.
  • Chapter 21 explains the structure of the document object, which is a container for most JavaScript elements such as forms, images, links, etc. An advanced programmer should master this chapter, because it is a cornerstone of JavaScript programming. Chapter 21 deals with only a few aspects of the document object (colors and output streams); other aspects are covered in other chapters. Chapter 21 formally explains output streams and output methods and their relationship with immediate and deferred scripts. Understanding output issues is very useful for advanced JavaScript programmers.
  • Chapter 22 is dedicated to HTML forms and form elements. It naturally deals with the forms array and the form object. The chapter includes a lengthy explanation of form validation, which is one of the most powerful features of JavaScript. In Chapter 22 you learn how to use various form elements for both collecting input and providing immediate responses. This chapter includes many example scripts.
  • Chapter 23 talks about links, anchors, and image maps. The hypertextual essence of a Web page is achieved by using these HTML constructs, which JavaScript can access and modify. Both novice and experienced programmers should be acquainted with this chapter. Links, anchors, and image maps are all related to linking two different documents or two points within a document, allowing fast and easy navigation between the two. You can use many different JavaScript methods to manipulate these native HTML elements.
  • Chapter 24 covers a very interesting concept in Web programming—cookies. Cookies allow storage of information on the client side for a specified duration. Due to the nature of these data tidbits, the storage of cookies is not affected by any standard operation performed by the user. For example, cookies are not deleted when the user shuts down his or her computer. Cookies should be studied in conjunction with forms, because it is usually convenient to use cookies for storing form-entered data.
  • Chapter 25 is intended for advanced programmers who are interested in using Netscape Navigator’s most powerful features. In this chapter you learn about images and how to use them with JavaScript. Chapter 25 focuses on the Image object and the document.images array. It teaches you how to cache images before they are loaded or used in an animation. This chapter is a must if you are interested in creating live, dynamic effects.
  • Chapter 26 describes frames. Frames let you divide your page into separate windows, each window being a target of a different document. You learn how to create frames with and without borders, how to use the event handlers for onFocus and onBlur, and how to emulate the focus and blur events. It also shows you how to target documents to the different frames. If you want to be considered an advanced page author, be prepared to learn frames and use them in your page.
  • Chapter 27 elaborates on windows. It teaches you how to open and close browser windows from JavaScript. It shows you how to customize your windows, for example, by omitting the status bar and menus. Chapter 27 introduces the concept of remote windows which Yahoo! was the first to feature. The code extract that generates a remote window is provided in this chapter. The onBlur and onFocus event handlers are described as well. A JavaScript-based HTML editor is explained in detail to demonstrate the usage of the features discussed in this chapter. Chapters 26 and 27 teach you most of what you need to know about browser windows and frames and how to manipulate them.
  • Chapter 28 deals with various number and string evaluation concepts in JavaScript. It also explains the differences between a function call and a function reference. It then shows how to take advantage of function references for compiling a function.
  • Chapter 29 explains how to use the navigator object to determine the user’s browser and platform. This information allows a Web page author to provide different content for each browser. Lastly, Chapter 29 elaborates on how to check if Java is currently enabled on the user’s browser. This is very important when implementing LiveConnect applications.
  • Chapter 30 shows how to embed a plug-in object in an HTML document (using the <EMBED> tag) and how to reference that plug-in from JavaScript. In this chapter, you learn how to determine which plug-ins are installed on the user’s browser. Finally, Chapter 30 discusses the basics of LiveAudio and LiveVideo.
  • Chapter 31 deals with LiveConnect, a feature of Netscape Navigator 3.0x and above. Using several examples, it shows how to use Java and JavaScript in concert. It also discusses the usage of LiveConnect to control LiveAudio and LiveVideo plug-ins from JavaScript.
  • Chapter 32 is dedicated to layers, a feature introduced in Netscape Navigator 4.0 (Communicator). It explains how to create layers with HTML and how to control them with JavaScript.
  • Chapter 33 describes JavaScript-accessible style sheets. This powerful capability, supported by Netscape Navigator 4.0 and above, lets you define the stylistic attributes of the page, including margins, padding, border width, and fonts. It explains how to create external style sheets, style sheets within the document, and style specification for specific elements. It also explains the mathematics of a text box, as far as its stylistic specifications are concerned.
  • Chapter 34 discusses security issues. The concept of data tainting is dealt with in depth. It allows users to choose which information to expose and which information should be secured. It presents the concept and use of the window taint accumulator for URL to URL secured access.
  • Chapter 35 focuses on a JavaScript authoring tool named Acadia Infuse. It shows ten top tips of Infuse, such as how to cleverly print your script, how to drag and drop new structures into your code, how to instantiate new objects, how to use special inline characters, visual trees, and tool bars, and how to reuse your own JavaScript codes.
  • Chapter 36, last but not least, helps you in debugging your scripts. It shows common error messages and what is usually the cause for each. It then suggests several techniques to find and fix bugs, such as viewing intermediate HTML files, reopening the browser, commenting out suspicious lines, and watching and tracing variables. It also suggests some tips on avoiding bugs in the first place by, for example, building the script skeleton first, and only then completing the details.
Previous Table of Contents Next


With any suggestions or questions please feel free to contact us