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 .. 39 40 41 42 43 44 < 45 > 46 47 48 49 50 51 .. 118 >> Next

To identify the referring page, you examine the referrer property of the document object, as shown in the following JavaScript code:
if (document.referrer == "”) {
document.writeln("You pulled this page up fresh in a browser.”);
else {
document.writeln("You were referred to this page by " + document.referrer);
The above code snippet determines the following:
If the value of document.referrer is blank (blank is denoted by "" in the code snippet), the user typed the name of the Web page directly into the browser address field.
If the value of document.referrer isn’t blank, document.referrer contains the name of the referring page.
The files detecting_referrer_base.htm and detecting_referrer.htm, which you find on the companion CD, allow you to test the code that I describe in this section. To use these files, upload them to a Web server, load the file detecting_referrer_base.htm in your Web browser, and click the link that appears.
You must upload your HTML files to a Web server in order to test the code that you see in this section; the value of document.referrer is always blank when tested locally.
122 Part II: Creating Dynamic Web Pages _
User preferences
Wouldn’t it be great if your users could choose they way they’d prefer to see your Web pages? Well, if you use JavaScript, they can! You can use JavaScript to present your users with a series of options right away, before your Web page loads — and then use that feedback to display your page the way your users want to see it.
In Figures 5-14 and 5-15, for example, you see prompts asking users which color they prefer for background and text color, respectively. Figure 5-16 shows the result: a Web page containing the user’s preferred color scheme.
Figure 5-14:
Asking users for their preferences.
Explorer User Prompt a]
Script Prompt 1 OK 1
What BACKGROUND color would you like? (red. green, white, yellow, etc.) Cancel |

Figure 5-15:
Your users can enter the text color.
Explorer User Prompt m)
Script Prompt 1 OK 1
What TEXT color would you like? (red, green, white, yellow, etc.) Cancel |

^ The code in Listing 5-3 is available on the companion CD: just load the file
Listing 5-3: Detecting User Preferences
<TITLE>Detecting user preferences (and customizing display)</TITLE>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
<!-- Hide from browsers that do not support JavaScript
// Ask the user for color preferences
var displayColor = prompt(”What BACKGROUND color would you like? (red, green, white, yellow, etc.)”, "pink”);
Chapter 5: Detecting Your Users' Browser Environments 123
var textColor = prompt("What TEXT color would you like? (red, green, white, yellow, etc.)”, "blue”);
// Display page content
document.writeln("<BODY BGCOLOR=” + displayColor + " TEXT=” + textColor + ">You chose " + textColor + " text on a " + displayColor + " background.</BODY>”)
// --> Finish hiding </SCRIPT>
As you skim through the JavaScript code in Figure 5-3, notice that it defines two variables:
displayColor, containing the user’s choice of background color textColor, containing the user’s choice of text color
After these two variables are defined, the JavaScript code uses them — along with the writeln() method — to define and display the <BODY> section of the Web page.
Figure 5-16:
A customized display based on user preferences.
124 Part II: Creating Dynamic Web Pages
Chapter 6
That's How the Cookie Crumbles
In This Chapter
^ Taking a close look at cookies
^ Understanding the benefits and limitations of cookies ^ Setting and retrieving cookie values
^ Creating a script to recognize previous visitors to your site
anlike a traditional client/server configuration, in which the client and the server have to agree to begin and end every conversation, the Web is stateless. Stateless means that, by default, neither Web browsers nor Web servers keep track of their conversations for later use. Like two ships that pass in the night, browsers and servers interact only when a user downloads a Web page, and then they immediately forget the other ever existed!
Cookies — tiny text files that a Web server can store on a client’s computer via a Web browser — were designed to change all that. By using cookies to keep track of browser-to-server interactions, Web developers can create intelligent Web sites that remember details about each and every user who visits them. You can even create cookies with built-in expiration dates so that information stored as cookies is maintained for only a limited period of time — say, a week or a month.
Cookie Basics
You can use JavaScript, Perl, VBScript, or any other Web-savvy language to store small text files called cookies on your site visitor’s computer. Because the whole point of using cookies is for server-side applications to keep track of client information, however, cookies are typically created and set by CGI programs rather than by JavaScript scripts. (CGI stands for Common Gateway Interface. CGI programs, which are usually written in Perl or C/C+ + , live on
Previous << 1 .. 39 40 41 42 43 44 < 45 > 46 47 48 49 50 51 .. 118 >> Next