in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 20 21 22 23 24 25 < 26 > 27 28 29 30 31 32 .. 118 >> Next

Here are a few examples of using literals in JavaScript:
alert("Sorry, you entered your e-mail address incorrectly.”)//string literal x = 1.31415 * someVariable // floating-point literal if (theAnswer == true) // boolean literal
document.write("The total number of users is " + 1234)//integer literal
Putting It All Together: Building JavaScript Expressions and Statements
In “JavaScript Syntax,” earlier in this chapter, you get familiar with the nuts and bolts of the JavaScript language. In this section, I demonstrate how to string these components together to create JavaScript expressions and statements.
JavaScript scripts are made up of JavaScript statements, which in turn are made up of JavaScript expressions. A JavaScript expression is any combination of variables, operators, literals (nonvarying values), and keywords that can be evaluated by the JavaScript interpreter.
For example, the following are all valid JavaScript expressions:
new Date()
numberSold * salesPrice
"Thanks for visiting my site, ” + document.myForm.yourName.value
These three examples are each slightly different, but they all have one thing in common: They can all be evaluated to something. The first example evaluates to the current date; the second, to a number; the third, to a string. (A string is a group of characters that you manipulate as a single block.)
Chapter 3: JavaScript Language Basics
To create a JavaScript statement, all you need to do is put together one or more JavaScript expressions (shown in bold in the following code). For example:
var todays_date = new Date();
calculateTotal(numberSold * salesPrice);
alert("Thanks for visiting my site, " + document.myForm.yourName.value);
In the first statement shown here, the current date is assigned to a variable called todays_date. In the second statement, the number produced by multiplying the numberSold and salesPrice variables is passed to the calculateTotal() function. And in the third example statement, the
"Thanks for visiting my site " string appears in a dialog box.
The difference between a JavaScript expression and a JavaScript statement might seem esoteric at first, but understanding this difference pays big dividends in the long run. It might help if you think of a JavaScript expression as a sentence fragment and a JavaScript statement as a complete sentence. Although an interoffice memo composed entirely of sentence fragments might not cause you any problems (unless your vocation happens to be teaching English), a JavaScript script composed of expressions does cause problems — in the form of runtime errors.
To prevent these errors (and to save the time you’d spend debugging them), you need to construct complete JavaScript statements. In the following sections, I use three useful scripts to demonstrate how to do just that.
The broWser-detection script
Back in the old days, before the Web came along, developers knew exactly what hardware and software their audience would use to run their applications before they wrote a lick of code. (In other words, these developers knew their applications’ target platforms in advance.) Using this information, developers could implement their applications with confidence, secure in the knowledge that their application code would behave in the field just as it did in their testing labs.
Not so on the Web. Users can choose to view Web pages with whatever target platform they choose. They might, for instance, use a Mac, a PC, a UNIX box, or a hand-held device running some version of Netscape Navigator, Internet Explorer, or any of the other dozens of Web browsers that are available on the market. Unfortunately, your users’ choices affect their ability to run your JavaScript-enabled Web pages, as you see in this chapter.
60 Part I: Building Killer Web Pages for Fun and Profit
Can't we all just get along? The ECMA standard
Netscape (with some help from Sun Microsystems) invented JavaScript clear back in the early 1990s, so it's no surprise that JavaScript support first appeared in Netscape's browser (Netscape Navigator 2.0, if you're a history buff).
Soon after, Microsoft released version 3.0 of Internet Explorer, which featured support for their own JavaScript-compatible scripting language — called JScript. Minor differences existed between these two browsers' scripting implementations, however, and as each successive version appeared, those differences continued to grow.
In 1998, Netscape decided to hand over the task of creating a formal JavaScript standard to the ECMA, an international standards body comprising companies from all over the world. (Both Netscape and Microsoft are ECMA members.) In theory, this was a great thing. It allowed a relatively impartial group of folks to decide the best, most efficient way to implement a cross-browser Web scripting language. Unfortunately — in software as in life — the reality of real-world implementation hasn't quite yet achieved the perfection promised by the standard.
Previous << 1 .. 20 21 22 23 24 25 < 26 > 27 28 29 30 31 32 .. 118 >> Next