Download JavaScript Variables and Data Manipulation and more Slides Artificial Intelligence in PDF only on Docsity! 1 CSCI 100 Think Like Computers Lecture 15 Fall 2008 Last Time … • JavaScript: THE scripting tool for webpages • For creating dynamic HTML webpages (DHTML) JavaScript Overview • <script type=“text/javascript> ……. // script code here </script> • <body onUnload=“alert(‘Thanks for visiting!’);” > • Variables VARIABLE = prompt("PROMPT MESSAGE", ""); firstName = prompt("Please enter your name", ""); • Write to the web page: document.write("MESSAGE TO BE DISPLAYED " + VARIABLE + " MORE MESSAGE" + ...); document.write("Hello " + firstName + ", welcome to my Web page."); Formatted Output • the output produced by a write statement is embedded in the page the browser displays this output just as it does any other text if the text contains HTML tags, the browser will interpret the tags and format the text accordingly document.write("Hello <i>" + firstName + "</i>, welcome to my Web page."); assuming the variable firstName has been assigned "Dave", the browser would execute the statement to produce Hello <i>Dave</i>, welcome to my Web page. which would be displayed by the browser as Hello Dave, welcome to my Web page. Syntax Errors • an error in the format of an HTML or JavaScript statements is known as a syntax error some syntax errors are ignored by the browser e.g., misspelling an HTML tag name most JavaScript syntax errors will generate an error message document.write("This example is illegal since the string is broken across lines"); yields: Error: unterminated string literal document.write("The value of x is " x); yields: Error: missing ) after argument list JavaScript Variables • a variable name can be any sequence of letters, digits, and underscores (but must start with a letter) valid: tempInFahr SUM current_age Sum2Date x invalid: 2hotforU salary$ two words "sum_to_date" • variable names are case sensitive, so Sum and SUM are treated as different variables Docsity.com 2 Variables & Memory Cells • computers keep track of the values that variables represent by associating each variable with a specific piece of memory, known as a memory cell when a JavaScript assignment is executed, firstName = prompt("Please enter your name", ""); the value entered by the user (e.g., "Dave") is stored in a memory cell associated with the variable firstName • "Dave" • firstName any future reference to the variable name evaluates to the value stored in its associated memory cell Reusing Variables • food = prompt("What is your favorite food?", ""); • document.write("Your favorite food is " + food + "<br />"); the first pair of statements • stores the user's favorite food • displays that food in the page Reusing Variables (cont.) • food = prompt("What is your least favorite food?", ""); • document.write("Your least favorite food is " + food + "<br />"); the second pair of statements • stores the user's least favorite food (overwriting the old value) • displays that food in the page Prompts with Defaults • so far, all prompts have been of the form VARIABLE = prompt("PROMPT MESSAGE", ""); • sometimes it makes sense to provide default values for prompts can specify a string literal instead of "" this string will appear in the prompt box when it appears if the user wants to accept the default value, can just click OK • EXAMPLE: suppose we wanted to create a page that displays a verse of the children's song, Old MacDonald had a Farm the page should be able to display any verse can accomplish this by prompting the user for the animal and sound can specify default values so that it is easy to display a common verse animal = prompt("Enter a kind of animal:", "cow"); sound = prompt("What kind of sound does it make?", "moo"); Old MacDonald Localizing Changes • so far, we have used variables to store values read in via prompts • another common use is to store values used repeatedly in a page suppose we wanted to change the spelling of the refrain in Old MacDonald ("E-I-E-I-O" "Eeyigh-Eeyigh-Oh") as is, would need to find and update all occurrences in the verse Docsity.com