InputDatafromUserSome common methods to input datafrom user:(1)Obtainuserinputwithpromptdialog.apromptdialog:allowsthe usertotypea value.Who?Johnvar who, wanted color:Which color?whowho=prompt("Who?',"):redwanted_color=prompt("Which color?', "'):wanted_color(2)Okay/ Cancel:Using confirmdialog.confirmdialogXMicrosoft Internet ExplorerAsksforOK/Cancel.Do you want to make it bold?. if (.) -. else ..Checksthetrue/falsevaluereturned bytheconfirmOKCanceldialogand performsthecorresponding action.Use+tocombinelines12-19inslide4if (confirm("Do you want to make it bold?"))document.write(Dear <b><span style-"color:' + wanted color + "",' + who + '</span></b>,"):elsedocument.write("Dear <span style="color:"+wanted color +"",'+who +'</span>"):http://www.cs.cityu.edu.hk/-helena2.ProgramStructureI-6Introductionto ComputerProgramming
Introduction to Computer Programming http://www.cs.cityu.edu.hk/~helena 2. Program Structure I - 6 (1) Obtain user input with prompt dialog • a prompt dialog: allows the user to type a value. Some common methods to input data from user: var who, wanted_color; who=prompt( ‘ Who? ’, ‘ ’ ); wanted_color=prompt( ‘ Which color? ’, ‘ ’ ); Which color? red Who? John who wanted_color (2) Okay / Cancel: Using confirm dialog if (confirm( "Do you want to make it bold?" )) document.write('Dear <b><span style="color:' + wanted_color + '">' + who + '</span></b>,'); else document.write('Dear <span style="color:' + wanted_color + '">' + who + '</span>,'); • confirm dialog Asks for OK / Cancel. • if (.) . else . Checks the true/false value returned by the confirm dialog and performs the corresponding action. Input Data from User Use + to combine lines 12-19 in slide 4
InputDatafromUserSome common methods to input data from user:(2) cont'd Okay/ Cancel:Using confirm dialogLec02_Slide07_BoldColoredNamelnvitation.html<html><head><title>Demo</title></head><body><scripttype-"text/javascript"varwho,wanted_color;who=prompt("Who?', '):wanted_color=prompt( "Which color?', “"):if (confirm("Do you want to make it bold?" ))document.write('Dear <b><span style="color:' + wanted_color + *",' + who +</span></b>,):elsedocument.write("Dear <span style="color:' + wanted_color + ",' + who +</span>,"):</script><br/><br/>Please come to my birthday party on May 18,2007<br/><br/>Seeyoul<br/><br/>Yours,<br/>Helena</body>
Introduction to Computer Programming http://www.cs.cityu.edu.hk/~helena 2. Program Structure I - 7 (2) cont’d Okay / Cancel: Using confirm dialog <html> <head> <title>Demo</title> </head> <body> <script type="text/javascript"> var who, wanted_color; who=prompt( ‘ Who? ’, ‘ ’ ); wanted_color=prompt( ‘ Which color? ’, ‘ ’ ); if (confirm( "Do you want to make it bold?" )) document.write('Dear <b><span style="color:' + wanted_color + '">' + who + '</span></b>,'); else document.write('Dear <span style="color:' + wanted_color + '">' + who + '</span>,'); </script> <br/><br/> Please come to my birthday party on May 18, 2007. <br/><br/> See you!<br/><br/> Yours,<br/> Helena </body> </html> Lec02_Slide07_BoldColoredNameInvitation.html Some common methods to input data from user: Input Data from User