- <SCRIPT LANGUAGE=”JavaScript”>. - <INPUT TYPE=”text” NAME=”entry”>. - <INPUT TYPE=”button” NAME=”oneButton” VALUE=”Press Me!”. - Not every object must have an event handler defined for it in the HTML, as shown in Listing 5-8 — only the ones for which scripting is needed. - The alertUser() function is defined as the page loads, and it waits to run as long as the page remains loaded in the browser. - Therefore, I will hold off on this example until later in the tutorial.. - In the early days of JavaScript in browsers, script errors displayed themselves in very obvious dialog boxes. - To prevent such dialog boxes from disturbing unsuspecting users, the browser makers tried to diminish the visual impact of errors in the browser win- dow. - appear in the status bar. - A message appears in the status bar that instructs you to go to the location javascript: to see the error details. - Viewing the details of the error requires dif- ferent steps, depending on the Navigator version. - Unless you clear the window, subsequent error messages are appended to the bottom of the window.. - During this tutorial, how- ever, you can use the error messages to see if you have perhaps mistyped a script from a listing in the book.. - simply sounds easier or more friendly than “programming.” In many respects, this is true. - In the end, both builders used many of the same techniques to complete the assembly, and each can take pride in the result.. - As you’ve seen with the document object model, the browser gives scripters many prefabricated components with which to work. - In the end, both authors have working applications that look equally professional.. - Beyond the document object model, however, “real programming” nibbles its way into the scripting world. - In the next two lessons, I set aside most discussion about the document object model and focus on the programming principles that will serve you well in JavaScript and future programming endeavors.. - Write the complete script tag set for a script whose lone statement is document.write(“Hello, world.”). - Build an HTML document and include the answer to the previous question such that the page executes the script as it loads. - Open the document in your browser.. - Add a comment to the script in the previous answer that explains what the script does.. - Carefully study the document in Listing 5-9. - sign in the upperMe function statement. - let the line word-wrap as it does in the follow- ing listing. - It’s okay to use a carriage return between attribute name/value pairs, as shown in the first <INPUT>. - Save the document as an HTML file, and load the file into your browser to see how well you did.. - document.converter.output.value = document.converter.input.value.toUpperCase(). - <FORM NAME=”converter”>. - <INPUT TYPE=”text” NAME=”input” VALUE=”sample”. - <INPUT TYPE=”text” NAME=”output” VALUE=””>. - By leaving out the “ava,” the company doesn’t have to license the “Java”. - The document that provides all of the details about the language is known as ECMA-262 (it’s the 262nd stan- dard published by ECMA). - The most serious discrepancies are noted in the core language reference in Part IV of this book.. - in the programming world, however, the term is not as restrictive. - Table 6-1 lists JavaScript’s formal data types, with examples of the values you will see displayed from time to time.. - In some definitions of syntax and parts of objects later in this book, I make specific reference to the type of value accepted in placeholders. - You will encounter situations, however, in which the value type may get in the way of a smooth script step. - If the script is to perform some arithmetic on that number, you must convert the string to a number before you can apply the value to any math operations. - Cooking up a dish according to a recipe in the kitchen has one advantage over cooking up some data in a program. - In the kitchen, you follow recipe steps and work with real things: carrots, milk, or a salmon fillet. - A computer, on the other hand, follows a list of instructions to work with data. - More specifically, data in a JavaScript- enhanced Web page occupies parts of the computer’s memory set aside for exclu- sive use by the browser software. - In the olden days, programmers had to know the numeric address in memory (RAM) where a value was stored to retrieve a copy of it. - That statement lets the browser know that you can use that variable later to hold information or to modify any of the data in that variable.. - To assign a value to a variable, use one of the assignment operators. - If I want to assign a value to the myAge vari- able at the same time I declare it (a combined process known as initializing the variable), I use that operator in the same statement as the var keyword:. - On the other hand, if I declare a variable in one statement and later want to assign a value to it, the sequence of statements is. - The designers of JavaScript, however, cannot foresee every keyword that the language may need in the future. - By using the kind of single words that cur- rently appear in the list of reserved keywords (see Appendix B), you always run a risk of a future conflict.. - Both of the following examples are valid variable names:. - In fact, because of the potential conflict with future keywords, using multiword combinations for variable names is a good idea. - Multiword combinations are less likely to appear in the reserved word list.. - Another concept closely related to the value and variable is expression evalua- tion — perhaps the most important concept of learning how to program a computer.. - Remember the theme song of The Beverly Hillbillies?. - At the end of the song, you find four quite different references (“crude,” “oil,”. - For exam- ple, after assigning a value to a variable, such as. - Therefore, if you’re 15 years my junior, I can assign a value to a variable representing your age based on the evalu- ated value of myAge. - myAge value changes later in the script, the change has no link to the yourAge vari- able because myAge evaluated to 45 when it was used to assign a value to yourAge. - Recall the second document.write() statement:. - document.write(“ of. - The document.write() method (remember, JavaScript uses the term method to mean command) requires a parameter in parentheses: the text string to be dis- played on the Web page. - The first evaluation is the value of the navigator.appName property. - This property evaluates to a string of the name of your browser. - With that expression safely evaluated to a string, JavaScript can finish the job of joining the three strings in the final evaluation. - As one more demonstration of the flexibility that expression evaluation offers, this section shows you a slightly different route to the document.write() statement.. - Rather than join those strings as the direct parameter to the document.write() method, I can gather the strings in a variable and then apply the variable to the document.write() method. - document.write(textToWrite). - This method works because the variable, textToWrite , evaluates to the com- bined string. - The document.write() method accepts that string value and does its display job
Xem thử không khả dụng, vui lòng xem tại trang nguồn hoặc xem
Tóm tắt