Slide 1
JavaScript Introduction
Joan Boone
jpboone@email.unc.edu
JavaScript Introduction Joan Boone jpboone@email.unc.edu Slide 1 - - PowerPoint PPT Presentation
INLS 572 Web Development JavaScript Introduction Joan Boone jpboone@email.unc.edu Slide 1 Topics Part 1: JavaScript Overview Part 2: Document Object Model (DOM) Part 3: Language Concepts Part 4: DOM Events Part 5: Show/Hide Content (See
Slide 1
Joan Boone
jpboone@email.unc.edu
Slide 2
Slide 3
JavaScript is a scripting language for building interactive and complex experiences on the web, such as responding to user interactions and updating dynamic content on a page. Anything involving how a web page should behave when an event
Slide 4
JavaScript is a client-side scripting language
when a page is loaded, or in response to a user action
client-side
Slide 5
Tutorials
Programming References (intermediate)
Plenty of free JavaScript available, but...
ECMAScript is a standard that forms the basis of JavaScript
Slide 6
Slide 7
called the Document Object Model
in your HTML document is represented as a node
and change every element in an HTML document
update the content, structure and style of documents.”
Slide 8
<html> <head> <title>My title</title> </head> <body> <a href="http://mylink.com”>My link</a> <h1>My header</h1> </body> </html>
HTML Document w3schools: HTML DOM Tree
Slide 9
Open the Inspector, and select the Elements tab (default view)
Slide 10
Your browser is an application that performs many tasks
You have to tell the browser where the JavaScript is located using <script> tags
it then hands over to the JavaScript interpreter.
Google Web Fundamentals: Constructing the Object Model
Slide 11
Two ways
<script></script> tags. Similar to embedding CSS with <style></style> tags.
Where to place your <script></script> tags?
Slide 12
Slide 13
Slide 14
Every programming language uses variables to manage and manipulate data; many languages also represent information as objects.
results of a calculation, or a reference to a DOM element
properties (attributes) and methods (functions) to manage them.
let todaysDate = "Monday, September 14, 2020"; let dateElement = document.querySelector("#todays-date");
DOM object that represents an HTML document (your web page). Method that finds the first HTML element that has an ID selector equal to todays-date DOM object that represents the
<p> element
<p id="todays-date">date will go here</p>
HTML: Java Script:
Simple variable that contains a text string
Slide 15 ... <p id="todays-date">date will go here</p> <script> let todaysDate = "Monday, September 14, 2020"; let dateElement = document.querySelector("#todays-date"); dateElement.textContent = todaysDate; </script> </body>
value of the current date
an element with the specified selector
<p> element with the ID selector, “todays-date”
content of an HTML element (in this case, <p>)
<p> contents are modified <p id="todays-date">Monday, September 14, 2020</p>
After script runs Before script runs
nc-national-parks-date.html
Slide 16 ... <p id="todays-date"></p> <script> let todaysDate = new Date(); let dateElement = document.querySelector("#todays-date"); dateElement.textContent = todaysDate; </script> </body>
you do not have to write the code yourself
variable in your script
A new Date object is created that returns a string (or text) representation of the current date and time
nc-national-parks-date-function.html
Slide 17
Instead of
Thu Sep 10 2020 18:14:10 GMT-0400 (Eastern Daylight Time)
Use toDateString() function
dateElement.textContent = todaysDate.toDateString(); Output: Thu Sep 10 2020
Or use month, day, year functions dateElement.textContent = todaysDate.getFullYear() + "." +
(todaysDate.getMonth()+1) + "." + todaysDate.getDate();
Output: 2020.9.10
w3schools: JavaScript Date Formats
Slide 18
Common scenarios
for invalid form inputs
Slide 19
Change the specified CSS property for dateElement w3schools: HTML DOM Style Object <p id="todays-date"></p> <script> let todaysDate = new Date().toDateString(); let dateElement = document.querySelector("#todays-date"); dateElement.textContent = todaysDate; dateElement.style.color="whitesmoke"; dateElement.style.fontStyle="italic"; dateElement.style.fontSize="smaller"; </script>
Slide 20
Quick exercise:
to one of your web pages
the style rules for the date
Slide 21
So far, the examples have used the querySelector method to obtain a reference to an HTML DOM element, and then saved it in a variable so that it can be modified.
<p id="todays-date">date will go here</p>
<script> let todaysDate = new Date(); let dateElement = document.querySelector("#todays-date"); dateElement.textContent = todaysDate; </script>
There is another JavaScript method, getDocumentElementById, that does the same thing, and you will often see this method used in examples and documentation.
<p id="todays-date">date will go here</p>
<script> let todaysDate = new Date(); let dateElement = document.getDocumentElementById("todays-date"); dateElement.textContent = todaysDate; </script> Reference: w3Schools HTML DOM querySelector() method
Slide 22
Open the Inspector, and select the Console tab
Problem: no date displayed, just the placeholder text
nc-national-parks-JS-debug.html
Slide 23
Slide 24
'listens' for an event, and when it occurs, a function is called to handle the event
Slide 25
let logo = document.querySelector('img'); logo.addEventListener('click', changeLogo); function changeLogo() { logo.style.filter = "invert(100%)"; logo.style.clipPath = "ellipse(50% 45% at 50% 50%)" logo.style.transform = "scale(1.2)"; }
nc-national-parks-click-event.html
<img src=".../landscape.png" alt="NC Landscape">
HTML JavaScript
Find the first img element and assign it to logo variable Add a 'click' event listener to the logo When the logo is clicked, call the
changeLogo function
The changeLogo function changes the style properties of the logo
Click event
Slide 26
let logo = document.querySelector('img'); logo.addEventListener('click', changeLogo); function changeLogo() { if (logo.style.filter == "invert(100%)") { logo.style.filter = "none"; logo.style.clipPath = "none"; logo.style.transform = "scale(1.0)"; } else { logo.style.filter = "invert(100%)"; logo.style.clipPath = "ellipse(50% 45% at 50% 50%)" logo.style.transform = "scale(1.2)"; } }
nc-national-parks-toggle-click-event.html If the current value of the logo filter is set to
"invert(100%)", then
reset the filter, clipPath and transform If the current value of the logo filter is “none”, then change the filter, clipPath, and transform
Click event
Slide 27
Toggle pattern behaves like a light switch: the switch is either on or off, and its state changes when you flip the switch up or down. Applying the toggle pattern to an HTML element on your web page allows you to change how the element is displayed when the user clicks it. Very generally, the steps include:
Initially the attributes are in state A, and after the click, the attributes are in state B.
when the element is clicked.
In the previous example, we saw how to change the style attributes of an image. A more common application of the toggle pattern is to show and hide content when an element is clicked.
Slide 28
Slide 29
Toggle pattern allows you to show or hide content based
someElement.style.display = “none” (hides the element) someElement.style.display = “block” (shows the element)
display:none or visibility:hidden
Slide 30
see-more-less.html, see-more-less.js
Slide 31
<div class="card1"> <img src=".../strawberries.jpg" alt="Strawberries" /> <p class="intro">Useful info about ...</p> <p id="details1">Lorem ipsum dolor sit amet, ... </p> <p id="moreLess1">See more</p> </div> let moreLess = document.querySelector('#moreLess1'); moreLess.addEventListener('click', toggleContent); function toggleContent() { let details = document.querySelector('#details1'); if (moreLess.textContent == "See more") { details.style.display = 'block'; moreLess.textContent = "See less"; } else { details.style.display = 'none'; moreLess.textContent = "See more"; } }
JavaScript
When the user clicks on See less, the
toggleContent function is called
In this scenario, the function will hide the details paragraph and change the clickable text to See more.
see-more-less.html, see-more-less.js
HTML
Slide 32
see-more-less-multiple.html see-more-less-multiple.js
Slide 33
Slide 34
An accordion is a user interface pattern that displays a list of headings that can show or hide additional content when selected. Because an accordion is a collection of headings that can be in show/hide states, this behavior is
the musical instrument. Important benefits
collapsing content
especially with complex applications
References
Slide 35
expand-collapse.html expand-collapse.js
Slide 36
<section id="topic3"> <header id="hdr3"> <i id="icon3" class="fas fa-plus"></i> <span>Publications</span> </header> <section id="details3"> <p>Lorem ipsum dolor ...</p> </section> </section>
expand-collapse.html, expand-collapse.js
... document.querySelector('#hdr3').addEventListener('click', toggleContent); function toggleContent(){ let icon, details; ... if (this.id == "hdr3") { icon = document.querySelector('#icon3'); details = document.querySelector('#details3'); } // Toggle display of details section and up/down caret icon if (details.style.display == 'block'){ // Is the details section showing? details.style.display = 'none'; // If yes, then hide it icon.className = 'far fa-angle-down'; // Change icon to down-caret } else { // The details section is hidden details.style.display = 'block'; // Show the details section icon.className = 'far fa-angle-up'; // Change icon to up-caret } }
HTML JavaScript
Slide 37
that can be customized with CSS
<i id="icon1" class="far fa-angle-down fa-lg"></i>
i {color: magenta; font-size: 0.8em; }
icon.className = "far fa-angle-up fa-lg";
Slide 38