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

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 .. 22 23 24 25 26 27 < 28 > 29 30 31 32 33 34 .. 118 >> Next

// == is a comparison operator
window.location = "netscape_version.htm”
// = is a comparison operator
}
// If the user is running some other browser,
// display a message and continue loading this generic // Web page. else {
document.write("You're not running Microsoft IE or Netscape.”)
}
}
// --> Finish hiding </SCRIPT>
</HEAD>
<BODY>
This is the generic version of my Web page.
</BODY>
</HTML>
The code that you see in Listing 3-3 combines comments, conditionals, and operators to create two complete JavaScript statements.
Part I: Building Killer Web Pages for Fun and Profit
As you read through the code, notice the following:
The appName property of the built-in navigator object is preloaded with one of two text strings: “Microsoft Internet Explorer” (if the loading browser is Internet Explorer) or “Netscape” (if the loading browser is Netscape Navigator).
^ Setting the window property of the location object equal to a new Web page causes that new Web page to load automatically.
Determining which brand of browser a user runs is relatively easy, as you can see by the code in Listing 3-3. However, determining the browser version is much trickier — and beyond the scope of this book. (Although the built-in navigator object does indeed contain useful properties such as appCodeName, appName, appVersion, userAgent, language, and platform — all of which you can display on-screen by using the alert() method — the contents of these properties are neither intuitive nor consistent between browsers.) For more information on browser-version detection, visit http://developer. netscape.com/docs/examples/javascript/browser_type_oo.html.
The date-formatting script
In Chapter 2, I introduce a simple date-and-time-stamp script that captures the current date and time and displays it on a Web page, like so:
Sat May 22 19:46:47 CDT 2004
In this section, I demonstrate how to combine comments, conditionals, operators, and variables into JavaScript statements that not only capture the current date and time but format the date and time so that they appear in a more human-friendly format, like the following:
Good evening! It's May 22, 2004 - 8:24 p.m.
To see how, take a look at the code in Listing 3-4.
You can find the code shown in Listing 3-4 on the companion CD by loading
up the list0303.htm file.
Listing 3-4: The Date-Formatting Script
<HTML>
<HEAD>
<TITLE>Displaying the current date and time (formatted example)</TITLE>
Chapter 3: JavaScript Language Basics
<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
<!-- Hide from browsers that do not support JavaScript
// Comments begin with //
// Get the current date
// The following statements declare variables var today = new Date();
// Get the current month var month = today.getMonth();
// Declare a variable called displayMonth var displayMonth='"';
// The following is a switch statement
// Attach a display name to each of 12 possible month numbers switch (month) { case 0 :
displayMonth = "January” break case 1 :
displayMonth = "February” break case 2 :
displayMonth = "March” break case 3 :
displayMonth = "April” break case 4 :
displayMonth = "May” break case 5 :
displayMonth = "June” break case 6 :
displayMonth = "July” break case 7 :
displayMonth = "August” break case 8 :
displayMonth = "September” break case 9 :
displayMonth = "October” break
(continued)
66 Part I: Building Killer Web Pages for Fun and Profit
Listing 3-4 (continued)
case 10 :
displayMonth = "November” break case 11 :
displayMonth = "December” break
default: displayMonth = "INVALID”
}
// Set some more variables to make the JavaScript code // easier to read
var hours = today.getHours(); var minutes = today.getMinutes(); var greeting; var ampm;
// We consider anything up until 11 a.m. "morning”
if (hours <= 11) {
greeting = "Good morning!”; ampm=”a.m.”;
// JavaScript reports midnight as 0, which is just
// plain crazy; so we want to change 0 to 12.
if (hours == 0) { hours = 12;
}
}
// We consider anything after 11:00 a.m. and before // 6 p.m. (in military time, 18) to be "afternoon”
else if (hours > 11 && hours < 18) { greeting = "Good afternoon!”; ampm = "p.m.”;
// We don't want to see military time, so subtract 12
if (hours > 12) {
hours -= 12;
}
}
Chapter 3: JavaScript Language Basics
// We consider anything after five p.m. (17 military) but // before nine p.m. (21 in military time) "evening”
else if (hours > 17 && hours < 21) {
greeting = "Good evening!”;
ampm = "p.m.”; hours - = 12;
}
// We consider nine o'clock until midnight "night”
else if (hours > 20) {
greeting = "Good night!”;
ampm = "p.m.”; hours - = 12;
}
// We want the minutes to display with "0” in front // of them if they're single-digit. For example,
// rather than 1:4 p.m., we want to see 1:04 p.m.
if (minutes < 10) {
minutes = "0” + minutes;
}
// + is a concatenation operator
Previous << 1 .. 22 23 24 25 26 27 < 28 > 29 30 31 32 33 34 .. 118 >> Next