Example 3-Asimple JavaScript program:EventDemo-Microsoft..Whenthepageis loaded,>3FileEditViewFavoritetheusertypesanameandatext color.InputBackThenthepagecontentissetupaccordinglyAddressProcessing and outputScriptPrompt:OKDemo-Microsoft Internet ExplorerName:EditFileViewFavoritesToolsHelpCancelavoritesJohnAddressDear John,Please come to my birthday party on May 18, 2007Script Prompt:OKSee you!Whichcolor?CancelYours,PurpleHelenaIntroductiontoComputerProgramming1.OverviewofProgrammingandJavaScript-6
Introduction to Computer Programming 1. Overview of Programming and JavaScript - 6 Example 3 - A simple JavaScript program: Name: John Which color? Purple When the page is loaded, the user types a name and a text color. Then the page content is setup accordingly. Event Input Processing and output
Let'sglanceatthe code (Example3):<html><head>口区Demo-Microsof...<title>Demo</title>FavoriteFileEdView</head>eAddres<body>ScriptPrompt:OK<scripttype="text/javascript"Name:Cancel... prompt("who?',""):John...prompt("Whichcolor?'"")e88888888800document.write(...):DaScript Prompt:OK</script>Please cormetomybirthdaypartyanMay18,2007CancelWhich color?SeeyadPurpleHekena</body></html>Explanation:. The input dialogs are invoked by prompt(..)·document.write(..)outputs the content in the page..EachJavaScriptstatementisendedwithasemi-colon:Forsimplicityfromnowonweomitthe<DOcTYPE>tagandthexmlnsattribueinthe<html>taginourcoursematerials
Introduction to Computer Programming 1. Overview of Programming and JavaScript - 7 <html> <head> <title>Demo</title> </head> <body> <script type="text/javascript"> . prompt('Who?', ''); . prompt('Which color?', ''); . document.write(.); </script> . . </body> </html> Explanation: • The input dialogs are invoked by prompt(.). • document.write(.) outputs the content in the page. • Each JavaScript statement is ended with a semi-colon: ;. Let’s glance at the code (Example 3): Name: John Which color? Purple For simplicity, from now on we omit the <!DOCTYPE> tag and the xmlns attribue in the <html> tag in our course materials