3.10. The coffee maker HTMLEnough coffee talk; it's time to build the asynchronous coffee maker application. First, let's take a look at the HTML for the coffee maker application, in coffee.html. We've gone ahead and written the HTML for placing and order and the first coffee maker (you're going to do the second coffee maker later). Let's see what you've got to work with: <html> <head> <title>Ajax-powered Coffee MakerAll this is standard stuff. We've linked to an external style sheet and included a title with the name of the application.</title> <link rel="stylesheet" type="text/css" href="coffee.css" /> </head> <body> <div > <h1>Ajax powered Coffee Maker</h1> </div> <div > <div > <h2>Coffee Maker #1</h2> <p><img src="/books/2/850/1/html/2/images/CoffeeMaker1.gif" alt="Coffee Maker #1" /></p> <div >Idle</div> </div>Here's the HTML for the first coffee maker. The status indicates if the coffee maker is idle or brewing someone's order. Notice we're using a <div> element to hold the status text. <div > This <div> is for ordering a cup of coffee. <p><img src="/books/2/850/1/html/2/images/coffeeMugWithBeans.jpg" alt="Coffee Pot 1" /></p> <h2>Place your coffee order here:</h2> <div > <form> <p>Name: <input type="text" name="name" /></p>Here's where you can select the size of the cup you want: Small, Medium, or Large. <h3>Size</h3> <p> <input type="radio" name="size" value="small" checked="true">Small</input> Here's where you enter your name, so the coffee maker knows who it's brewing coffee for. x <input type="radio" name="size" value="medium">Medium</input> <input type="radio" name="size" value="large">Large</input> </p>...continued on the next page... <h3>Beverage</h3> <p> <input type="radio" name="beverage" value="mocha" checked="true">Mocha</input> <input type="radio" name="beverage" value="latte">Latte</input> <input type="radio" name="beverage" value="cappucino">Cappucino</input> This is the last of the coffee order options: you can select the type of coffee you want. </p> <p> <input type="button"Here's the button to place a coffee order. We'll come back to this and fill in the JavaScript onClick event handler a bit later. onClick="__________________" value="Order Coffee" /> </p> </form> </div> </div> <div > <h2>Coffee Maker #2</h2> <p><img src="/books/2/850/1/html/2/images/CoffeeMaker2.gif" alt="Coffee Maker #2" /></p> <div >Idle</div>Later in the chapter, we'll come back and finish the HTML for the second coffee maker. </div> <p ></p> This is a little HTML and CSS trick to make sure the coffee ordering options all fit into the main white part of the form. </div> </body> </html> Before we start working on the Javascript, I was thinking... Wouldn't it be better to put our Javascript in a separate file instead of always mixing it in with our HTML? 3.10.1. Yes, let's put your JavaScript in a separate file.Instead of putting all the JavaScript for the coffee maker directly in the HTML, let's put it in a separate file. Then we can link to it using a <script> element in the <head> of the HTML for the coffee maker. Just like separating your HTML and CSS is a good idea, separating your Javascript from your HTML is also a good idea: it keeps the logic (the Javascript) of your application separate from the app's structure and presentation (the HTML and CSS). You know, I think we should actually make two filesone for the JavaScript that's the same in all our Ajax applications, and one for the JavaScript that's specific to the coffee maker. Then we can use the common JavaScript file any time we write a new application. You're probably tired of typing the same JavaScript over and over for each application... we can take care of that by creating two files for our JavaScript: one for the JavaScript that stays the same for every Ajax application, and one for the JavaScript that's specific to an application. Let's call the JavaScript file containing the code that's common to every application ajax.js, and the file containing the code specific to this Coffee Maker application coffee.js . Go ahead and update your HTML with two <script> elements linking to the two JavaScript files you're about to create: <html> <head> <title>Ajax-powered Coffee Maker</title> <link rel="stylesheet" type="text/css" href="coffee.css" /> <script type="text/javascript" src="/books/2/850/1/html/2/ajax.js"> </script> <script type="text/javascript" src="/books/2/850/1/html/2/coffee.js"> </script> Here are the two <script> elements you need to link to the two JavaScript files. Be sure to add the empty space and the </script> tag, or some browsers won't load the JavaScript you point to. </head> <body> ... </body> </html>
|