Introduction to Javascript
ATLS 3020 - Digital Media 2 Week 2 - Day 2
Introduction to Javascript ATLS 3020 - Digital Media 2 Week 2 - Day - - PowerPoint PPT Presentation
Introduction to Javascript ATLS 3020 - Digital Media 2 Week 2 - Day 2 Overview 1. What is javascript? 2. Output 3. Variables 4. Comments 5. Input 6. Putting it all together What is javascript? Output Variables Comments Input Putting
ATLS 3020 - Digital Media 2 Week 2 - Day 2
What is javascript? Output Variables Comments Input Putting it all together
HTML CSS HTML CSS Javascript
What is javascript? Output Variables Comments Input Putting it all together
○
Ham ≠ Hamburger
○
Java ≠ Javascript
○
Runs entirely in the web browser (not on the server)
○
Official language for HTML 5
○
Runs in all major web browsers
HTML PHP SQL/MySQL Javascript/jQuery CSS ajax
Front-End Back-End Client Server
What is javascript? Output Variables Comments Input Putting it all together
What is javascript? Output Variables Comments Input Putting it all together
① Directly to the HTML page ② In an external file
<body> <h1>This is my webpage</h1> <script> alert("Hello!"); </script> </body> HTML index.html <body> <h1>This is my webpage</h1> <script src="script.js"></script> </body> HTML index.html alert("Hello!"); Javascript script.js
What is javascript? Output Variables Comments Input Putting it all together alert("Hello!"); Javascript document.write("Hello!"); Javascript
① Dialog box ② Print to webpage
○ “Hello!” ○ ‘I hope it doesn’t rain today’ ○ “123456790” ○ ‘! Alert # 4 !’
What is javascript? Output Variables Comments Input Putting it all together
○ alert box ○ print directly to webpage
Example webpage: creative.colorado.edu/~kosba/dm2/tutorials/w2-2/outputs Download example code: creative.colorado.edu/~kosba/dm2/tutorials/w2-2/outputs.zip What is javascript? Output Variables Comments Input Putting it all together
What is javascript? Output Variables Comments Input Putting it all together
What is javascript? Output Variables Comments Input Putting it all together
① Create the variable ② Assign it a value
var firstName; Javascript firstName = "Brittany"; Javascript
Or these can be combined into 1 line of code (but note there is still 2 steps happening).
var firstName = "Brittany"; Javascript
What is javascript? Output Variables Comments Input Putting it all together
This . . .
var firstName; firstName = "Brittany"; document.write(firstName); Javascript
is the same as this:
document.write("Brittany"); Javascript
Example webpage: creative.colorado.edu/~kosba/dm2/tutorials/w2-2/variables Download example code: creative.colorado.edu/~kosba/dm2/tutorials/w2-2/variables.zip What is javascript? Output Variables Comments Input Putting it all together
What is javascript? Output Variables Comments Input Putting it all together
Comment syntax depends on the language
What is javascript? Output Variables Comments Input Putting it all together <!-- This is a comment --> <h1>Welcome to my webpage!</h1> HTML /* This is a comment */ .red { color: red; } CSS // This is a comment document.write("Brittany"); Javascript /* This is a multi-line comment */ document.write("Brittany"); Javascript /* * This is also a multi-line comment */ document.write("Brittany"); Javascript
Javascript comments can be added in 2 ways: ① Single-line comment ② Multi-line comment
Example webpage: creative.colorado.edu/~kosba/dm2/tutorials/w2-2/comments Download example code: creative.colorado.edu/~kosba/dm2/tutorials/w2-2/comments.zip What is javascript? Output Variables Comments Input Putting it all together
What is javascript? Output Variables Comments Input Putting it all together
Javascript can “ask” the user for input in many ways, here is 1 example:
What is javascript? Output Variables Comments Input Putting it all together prompt("What is your name?"); Javascript
Why doesn’t this do anything? We need to store the response.
var name; name = prompt("What is your name?"); Javascript
① Create a variable ② Store the value of “prompt” in that variable
We can use this variable just like any other variable we’ve created.
What is javascript? Output Variables Comments Input Putting it all together var name; name = prompt("What is your name?"); document.write(name); Javascript
We can combine strings!
var name; name = prompt("What is your name?"); document.write("Hello, " + name + ". How are you?"); Javascript
What if we wanted to write: “Hello, <name>. How are you?”
Example webpage: creative.colorado.edu/~kosba/dm2/tutorials/w2-2/inputs Download example code: creative.colorado.edu/~kosba/dm2/tutorials/w2-2/inputs.zip What is javascript? Output Variables Comments Input Putting it all together
○
both types of outputs (dialog box and printed directly to HTML)
○
at least one single-line comment and one multi-line comment
○
at least two “prompts”
○
at least two variables (your “prompts” should be stored with different variables)
include a javascript file, your choice.
What is javascript? Output Variables Comments Input Putting it all together