Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps?...

29
Enhancing Websites with JavaScript

Transcript of Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps?...

Page 1: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Enhancing Websites with JavaScript

Page 2: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

How Do You Give Directions?

• Lots of details?

• Specific steps?

• Just the gist?

• What language?

Page 3: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Giving Directions With JavaScript

• Just one way of talking to a computer.

• Not related to Java

• Useful for games and widgets

• Useful for websites

Page 4: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Just the Basics

• Format

• Output

• Variables

• Functions

• Input

• Conditionals

Page 5: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Format

• Goes in head or body or another tag or another file

• Uses <script> </script> tags

• Need to specify the language in the <script> tag

• <script type=“text/javascript”> </script>

Page 6: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Output

• Statements that tell the computer to print something somewhere.

• document.write(WhateverYouWantToPrint)

Page 7: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Output Example

<html><head><title>First JavaScript page</title></head><body><script type="text/javascript">

document.write("<h1>Hello World</h1>");</script></body></html>

Page 8: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Resulting Webpage

Page 9: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Hello Part 2

<html><head><title>First JavaScript page</title></head><body><script type="text/javascript">

document.write("<h1>Hello World</h1>");document.write("<h2>Or Whoever Is Listening</h2>");

</script></body></html>

Page 10: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

And the Webpage

Page 11: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Why Use JavaScript?

What if we wanted to calculate 2*3?

<html><body>2*3=2*3</body></html>

Page 12: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

And the Resulting Webpage

Page 13: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

JavaScript Solution

<html><head><title>First JavaScript page</title></head><body><script type="text/javascript">

answer = 2*3;document.write("2*3="+ answer);

</script></body></html>

Page 14: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Resulting JavaScript Webpage

Page 15: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Another Calculating Example

<html><head><title>First JavaScript page</title></head><body><script type="text/javascript">

timesanswer = 2*3;plusanswer = 2+3;document.write("2*3="+ timesanswer + " and 2+3=" + plusanswer);

</script></body></html>

Page 16: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Along With the Webpage

Page 17: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

What if We Want Three 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>

Page 18: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

What if 100’s of Calculations?

• Or lots of something else beside calculations?

• Functions are an answer!

• Functions can go in the head and be called from the body.

Page 19: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

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>

Page 20: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

First Function Webpage

Page 21: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Not Much Fun Without User Input

• Can use <form> </form> tags• Inside the form tags use <input> tag• Some types of input

Text Button Checkbox

• Types have attributes such as size and name• Can respond back to the user with an Alert (tiny

popup window)

Page 22: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Sample User Input Page

<html><head><title>First JavaScript page</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>

Page 23: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Resulting Webpage

• Check it out at http://www.cs.mtsu.edu/~pettey/1150/form.html

Page 24: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

What About Conditions?

• If something happens, do something, otherwise do something else...

• That is the computer can do different things based on some decision.

Page 25: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Try Out and Example Without Decision Making

• The following website does division of two numbers. Try it out. Be sure to try and divide by 0.

• http://www.cs.mtsu.edu/~pettey/1150/form2.html

Page 26: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

How to do a JavaScript Decision

If (something happens){take some action

}else{

take a different action}

Page 27: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

Something Happens?

Symbol meaning

== Equal to, and yes that is two = signs right next to each other

!= Not equal to

> Greater than

< Less than

>= Greate than or equal to

<= Less than or equal to

Page 28: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

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.

Page 29: Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?

More Examples

• Calculate the average of three test scores http://www.cs.mtsu.edu/~pettey/1150/form4.html

• Read info and send email if over 18 http://www.cs.mtsu.edu/~pettey/1150/email.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