1 / 22

Introduction to JavaScript

Monday Exam on Web Dev and Data Comm Study Guides on D2L Bring Small Scantron. Introduction to JavaScript. Why Do We Need JavaScript. With HTML and CSS your web site can’t: Accept user input Perform arithmetic operations Can’t alter their actions based on a logic

lotta
Download Presentation

Introduction to JavaScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Monday Exam on Web Dev and Data Comm Study Guides on D2L Bring Small Scantron Introduction to JavaScript

  2. Why Do We Need JavaScript With HTML and CSS your web site can’t: • Accept user input • Perform arithmetic operations • Can’t alter their actions based on a logic • JavaScript is one of the programming languages that can be used to add those capabilities

  3. JavaScript • Programming language commonly used for web site features. • Useful in developing: • Games • Widgets • Websites • Not related to Java

  4. JavaScript Code In a Web Page <html> <head> <title>First JavaScript Program</title> </head> <body> <script type="text/javascript"> alert("Another Annoying Pop-up"); </script> </body> </html> • Javascript program code can be placed within a web page <head> or <body> section, or in a separate file • The JavaScript code is delimited by the tags: <script type=“text/javascript”> and </script> • JavaScript statements end with a semicolon

  5. JavaScript Output • There are two statements that can produce output • alert – creates a Pop-Up window including the specified text (prior example) • document.write – sends a line of output for the browser to interpret document.write(“Whatever You Want”);

  6. document.write Example • Notice that it acted on the <h1> tag • document.writepresents it’s contents to the browser for its interpretation.

  7. document.write Example 2

  8. Constants and Simple Variables • Constants maintain a fixed value 2 3 Joe • Variables are buckets that hold values, and can change

  9. JavaScript Math JavaScript provides the ability to do math within a web page Basic math operators are +, -, *, / answer = 2*3; The variable named answer would contain the value of 6

  10. JavaScript Math Example The quotes indicate a constant. + sign concatenates the character strings

  11. What if We Want Three of the Same Calculations? <html> <head> <title>First JavaScript page</title> </head> <body> <script type="text/javascript"> answer = 2*3; document.write("2*3="+ answer + "<br><br>"); answer = 10*5; document.write("10*5="+ answer + "<br><br>"); answer = 1024*4; document.write("1024*4="+ answer + "<br><br>"); </script> </body> </html>

  12. What if 100’s of Calculations? Or lots of something else that requires the same instructions beside calculations? Recoding the same statements is time consuming, error prone, and no fun. Functions are an answer! Functions are reusable snippets of code that accept input and produce results/actions. Function definitions go in the <head> section and are called from within the <body> section.

  13. Example Using a Function to Calculate <html> <head> <title>First JavaScript page</title> <script type="text/javascript"> function calculateAnswers(number1,number2 ){ timesanswer = number1*number2; document.write(number1 + "*" + number2 + "=" + timesanswer); document.write("<br><br>"); } </script> </head> <body> <script type="text/javascript"> calculateAnswers(2,3); calculateAnswers(10,5); calculateAnswers(1024,4); </script> </body> </html> Parameters communicate values

  14. First Function Webpage

  15. Forms Accept User Input • <form> </form> tags delimit a form definition • Inside the form tags use <input> tag • Some types of input • Text • Button • Checkbox • Types have attributes such as size and name

  16. Sample User Input Page <html> <head> <title>First Form</title> <script type="text/javascript"> function calculateAnswers(number1,number2){ timesanswer = number1*number2; alert("The answer is: " + timesanswer); } </script> </head> <body> Enter two numbers to be multiplied: <form> <p><b>Number1:</b> <input type="TEXT" size="10" name="num1"> <p><b>Number2:</b> <input type="TEXT" size="10" name="num2"> <p><input type="BUTTON" value="Calculate Answer" onClick="calculateAnswers(num1.value, num2.value);"</p> </form> </body> </html> Check it out at http://www.cs.mtsu.edu/~pettey/1150/form.html

  17. Form Results

  18. JavaScript Supports Logic (Decisions) • If something happens, • do something, • otherwise do something else... • That is the computer can do different things based on some decision. • In Javascript language: • If (something happens){ • take some action; • } • else{ • take a different action; • }

  19. Conditionals

  20. Ifs if (answer==“yes”) { document.write(“I agree”); } else { document.write(“I disagree”); } if (answer==“yes”) { document.write(“I agree”); } else if (answer==“no”) { document.write(“I disagree”); } else { document.write(“make up your mind”); }

  21. Example With Decision Making • Check out http://www.cs.mtsu.edu/~pettey/1150/form3.html • Be sure to view the page source to see the condition.

  22. More Examples • You’ll find a collection of them including those detailed below at: http://www.cs.mtsu.edu/~djoaquin • Calculate the average of three test scores http://www.cs.mtsu.edu/~pettey/1150/form4.html • Vote with alert after each button click http://www.cs.mtsu.edu/~pettey/1150/vote.html • Vote with alert only after announce the winner button is clicked http://www.cs.mtsu.edu/~pettey/1150/vote2.html • Read info and send email if over 18 http://www.cs.mtsu.edu/~pettey/1150/email.html

More Related