Session 08 - Javascript, Part 1
Harvard Extension School
Spring 2022
Course Web Site: https://cscie12.dce.harvard.edu/
Topics
- JavaScript
- Getting Started with JavaScript
- JavaScript and HTML
- Document Object Model (DOM)
- JavaScript and Ice Cream
Presentation contains 19 slides
JavaScript
Goals for the JavaScript Unit- Understand basics of JavaScript
- events
- DOM
- Troubleshooting console
- Essential operations:
- DOM manipulations
- if...else
- for and iterations
- lists, objects, JSON structure
- Understand how to use JavaScript
- Separate JS from Markup
- Be able to incorporate JavaScript functionality into a site
- Be able to use JavaScript for commonly needed functionality
- Ready for more!
Client-side Web Parts: Markup, Style, Function
| Structure + Style + Function: solarsystem.html
Structure + Style: solarsystem-style.html
Structure: solarsystem-markup.html
|
Javascript
"JavaScript is now a language every developer should know."
– Mike Loukides, Vice President of Content Strategy for O'Reilly Media
JavaScript is everywhere: servers, rich web client libraries, HTML5, databases, even JavaScript-based languages.
If you've avoided JavaScript, this is the year to learn it.
And if you don't, you risk being left behind.
What is it?
- Programming language
- Runs within web browser
- Browser runs Javascript based upon events
- [Server-side language as well!]
What can it do?
Javascript provides programmatic access to virtually all aspects of a page.
- CSS properties
- Markup content
- Forms, communication to Server
- Add functionality
ECMAScript and JavaScript
The standard for JavaScript is called ECMAScript.
Javascript Resources
- JavaScript - Mozilla Developer Center
- JS Reference (DevDocs)
- JavaScript Tutorial and Reference from w3schools.com
- JavaScript for Web Designers, a short book available online through the Harvard Library.
- Jennifer Robbins, 2018. Learning Web Design: A Beginner's Guide to HTML, CSS, JavaScript, and Web Graphics, 5th ed. O'Reilly Media. 500 p. ISBN 978-1491960202
Javascript: Manipulate Styles
- Manipulate Styles / Classes
- Manipulate Content
- Communicate with Web Server to Get and Post data
- Add functionality such as maps, galleries, etc.
Javascript: Manipulate Content
- Manipulate Styles / Classes
- Manipulate Content
- Communicate with Web Server to Get and Post data
- Add functionality such as maps, galleries, etc.
Javascript: Communicate with Web Server
- Manipulate Styles / Classes
- Manipulate Content
- Communicate with Web Server to Get and Post data
- Add functionality such as maps, galleries, etc.
Javascript: Add functionality
- Manipulate Styles / Classes
- Manipulate Content
- Communicate with Web Server to Get and Post data
- Add functionality such as maps, galleries, etc.
Getting Started with JavaScript
- JavaScript - Mozilla Developer Center
- JS Reference (DevDocs)
- JavaScript Tutorial and Reference from w3schools.com
- JavaScript for Web Designers, a short book available online through the Harvard Library.
- Jennifer Robbins, 2018. Learning Web Design: A Beginner's Guide to HTML, CSS, JavaScript, and Web Graphics, 5th ed. O'Reilly Media. 500 p. ISBN 978-1491960202
Some basics
- JS in HTML. Including JavaScript in HTML with
script
element, with eithersrc
attribute or JS code. - Variables. Declare variables with
let
and sometimesconst
.
Though you will see the oldervar
, but generally avoid that. - Console is your friend. Your browser developer tools will be critical to use, and
especially the "Console". Log to the console in JS with
console.log('hello there!');
- When is the code executed?
script
inhead
or right before the body close tag -</body>
But it is good practice to rely on specific events! - Events - load, click, focus, blur, submit, mouseover, mouseout, keypress.
HTML attributes are available, but don't use these. Add event listeners through JavaScript!
JavaScript and HTML
- Scripting (HTML Living Standard)
<script>
element- Can reference an external JS file (
src
attribute) - JS code can be within
script
element
- Can reference an external JS file (
<script>
element can go inhead
, but is often placed just before the body close (i.e.</body>
) elements.- Note the
<noscript>
element for clients without JavaScript.
Script element in HTML/XML syntax should have separate end tag!
script
element should have an explicit end tag.
Best:
<script src="jquery.js" > </script>
Do not use:
<script src="jquery.js" />
JavaScript in HTML
- External Script
<script src="path/to/javascript/file.js" > </script>
- Script within HTML document
<script> /* JavaScript code as content of script element */ </script>
Events
- Events - things that happen on page
- Event Handlers - code that is run when an event happens
- Registering an Event Handler - linking the code you want to run to an event
Working with Events
addEventListener()
DOMContentLoaded
- Events:
click
,submit
,change
,focus
- And more events:
blur
,mouseover
,mouseout
,keypress
,mousedown
, and more
- And more events:
addEventListener function lets us add Event Listeners via JavaScript!
let myButton = document.getElementById("makelistbutton");
myButton.addEventListener("click", createMyListOfFruits);
JavaScript Functions
Named Functions
function davidsFunction() {
}
Anonymous Functions
function() {
}
Functions with arguments
function calcContrast(color1, color2) {
let contrast;
/* contrast calculation..... */
return(contrast);
}
calling a function:
let myContrast = calcContrast('a51c30','ffffff');
Document Object Model (DOM)
<html lang="en">
<head>
<title>My Schools</title>
</head>
<body>
<h1>My Schools</h1>
<ul>
<li>
<a href="http://www.harvard.edu/">Harvard University</a><br/>
<img src="images/veritas.gif" alt="Harvard Shield" height="84" width="72"/>
</li>
<li>
<a href="http://www.ku.edu/">University of Kansas</a><br/>
<img src="images/KUSeal.gif" alt="University of Kansas Seal" height="73" width="72"/>
</li>
</ul>
</body>
</html>
DOM Visualizer
JavaScript and DOM
getElementsById
getElementsByTagName
querySelector
lets us use "CSS selector" syntax and grab the "first" occurence.querySelectorAll
lets us use "CSS selector" syntax and grab the "first" occurence.getAttribute
andsetAttribute
classList
andadd
,remove
,toggle
- Various DOM methods to create nodes and add to the node tree (not today!)
JavaScript and Ice Cream
Exposing Additional Form Elements
Additional relevant form elements are exposed based upon user input.
Here, this is achieved by having the entire form in the markup, with a certain section
hidden via CSS (display: none;
). If "Yes" is chosen,
the display property is changed to "block" through JS.
<form method="post" name="ice_cream" id="ice_cream" action="https://cs12.net/form/submit.php">
<div>Would you like ice cream?
<label>
<input type="radio" name="want" id="ic_yes" value="yes"/>
Yes </label>
<label>
<input type="radio" name="want" id="ic_no" value="no"/>
No </label>
</div>
<fieldset id="icecream_options">
<legend>Ice Cream Options </legend>
<p>Cup or a Cone?
</p>
<label>
<input type="radio" id="container_cup" name="container" value="cup"/>
Cup </label>
<label>
<input type="radio" id="container_cone" name="container" value="cone"/>
Cone </label>
<p>Pick your toppings:
</p>
<label>
<input type="checkbox" name="toppings" id="toppings_wc" value="whipcream"/>
Whipped cream </label>
<label>
<input type="checkbox" name="toppings" id="toppings_jimmies" value="jimmies"/>
Jimmies </label>
<label>
<input type="checkbox" name="toppings" id="toppings_sprinkles" value="sprinkles"/>
Sprinkles </label>
<label>
<input type="checkbox" name="toppings" id="toppings_nuts" value="nuts"/>
Nuts </label>
<label>
<input type="checkbox" name="toppings" id="toppings_cherry" value="cherry"/>
Cherry </label> </fieldset>
<button type="submit">Submit Order </button>
</form>
In style
element
(<style>
) within head
element:
#icecream_options {display:none;
background-color: #eee; margin-left: 2em;
} #icecream_options.show { display: block; } label { display: block; }
In
script
element within head
element (<script>
):
document.addEventListener("DOMContentLoaded", function() {
let inputYes = document.getElementById('ic_yes');
inputYes.addEventListener("click", function() {
displayIceCreamOptions();
});
let inputNo = document.getElementById('ic_no');
inputNo.addEventListener("click", function() {
displayIceCreamOptions();
});
});
function displayIceCreamOptions() {
let divICO = document.getElementById('icecream_options');
let inputYes = document.getElementById('ic_yes');
if (inputYes.checked == true) {
divICO.classList.add("show");
} else {
divICO.classList.remove("show");
}
}