javascript introduction
play

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


  1. INLS 572 Web Development JavaScript Introduction Joan Boone jpboone@email.unc.edu Slide 1

  2. 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 more/less) Part 6: Expand/Collapse Content (Accordion pattern) Slide 2

  3. What is JavaScript and how is it used? 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 occurs is what JavaScript is used for. Some examples.... • Slideshows: Suzie Wolf Photography, Art Wolfe • Form validation: awwwards Contact Us • Calculators: Google calculator, PX to EM conversion Graphs: COVID-19 US Cases, Financial Times Markets Data • Google Maps • Slide 3

  4. JavaScript Runs in the Browser client-side JavaScript is a client-side scripting language  Scripts are embedded/referenced in a web page, and executed when a page is loaded, or in response to a user action  It is NOT the same as the Java programming language  Java is compiled (and much faster); JavaScript is interpreted  TIOBE Programming Index Slide 4

  5. JavaScript – Getting Started Tutorials  w3schools: JavaScript Tutorial  MDN web docs: JavaScript  Codecademy, Coursera Programming References (intermediate)  Robust Client-Side JavaScript: A Developer's Guide  Eloquent JavaScript  JavaScript for Web Designers Plenty of free JavaScript available, but...  Need to understand the free stuff before you can use it  Free JavaScript is not necessarily good JavaScript  Check the dates on the web sites, demos, and examples ECMAScript is a standard that forms the basis of JavaScript Slide 5

  6. 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 more/less) Part 6: Expand/Collapse Content (Accordion pattern) Slide 6

  7. JavaScript supports interaction by manipulating the Document Object Model (DOM) • Every HTML document has an underlying representation called the Document Object Model • The DOM is a hierarchical tree structure where everything in your HTML document is represented as a node  entire document  each element, and each attribute of an element  text in an element • The DOM is a W3C standard that defines a way to access and change every element in an HTML document  “...allow programs and scripts to dynamically access and update the content, structure and style of documents.” Slide 7

  8. Document Object Model (DOM) HTML Document <html> <head> <title>My title</title> </head> <body> <a href="http://mylink.com”>My link</a> <h1>My header</h1> </body> </html> w3schools: HTML DOM Tree Slide 8

  9. Viewing the DOM for a web page Open the Inspector, and select the Elements tab (default view) Slide 9

  10. How browsers process a web page Your browser is an application that performs many tasks  Creates a model of a page (DOM and CSSOM)  Uses a rendering engine to render and lay out your web page based on the HTML document and CSS style rules  Interprets and runs your JavaScript You have to tell the browser where the JavaScript is located using <script> tags  Tags tell the browser that there is some JavaScript code to run, which it then hands over to the JavaScript interpreter.  When the interpreter is done, control returns back to the render task Slide 10 Google Web Fundamentals: Constructing the Object Model

  11. How to include JavaScript in a web page Two ways • Embed JavaScript code within your page by delimiting with <script></script> tags. Similar to embedding CSS with <style></style> tags. • Put JavaScript in a separate file  Use <script> tags with a src attribute that specifies the file  Example: <script src="myJavaScript.js"></script> Where to place your <script></script> tags? Recommendation: at the end of your web page, before </body> tag • • Reduces rendering delays and may improve performance Slide 11

  12. 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 more/less) Part 6: Expand/Collapse Content (Accordion pattern) Slide 12

  13. JavaScript Language Concepts • Objects, variables, and data types  Object often represent elements in the Document Object Model (DOM)  Variables act a containers for values  Values have a data type: number, string, booleans, DOM element • Statements: separated by semi-colon (recommended, not required) • Operators  Arithmetic: +, -, *, /, ++, –, %  Comparison: ==, !=, >, <, >=, <=  Logical: AND (&&), OR (||), and NOT (!) • Conditionals: if/else statements to evaluate true/false conditions • Iteration: repeatedly executing statements in a loop • Functions: built-in or user-defined • Events: actions that can be detected with JavaScript  Clicking buttons, mouse hover, input changes, page loading Slide 13

  14. JavaScript Objects and Variables Every programming language uses variables to manage and manipulate data; many languages also represent information as objects. • Variables are used to contain information that your script uses , e.g., numbers, text, results of a calculation, or a reference to a DOM element • The browser stores the values of your variables Variables are often declared using the let keyword • • Objects are often used to represent more complex kinds of information. They have properties (attributes) and methods (functions) to manage them. HTML: <p id="todays-date">date will go here</p> Java let todaysDate = "Monday, September 14, 2020"; Script: let dateElement = document.querySelector("#todays-date"); Simple variable DOM object that DOM object that Method that finds the first HTML that contains a represents the represents an element that has an ID selector equal text string <p> element HTML document to todays-date (your web page). Slide 14

  15. JavaScript Variables and Modifying Content • todaysDate is a string variable that contains the value of the current date querySelector is a DOM method that returns • an element with the specified selector dateElement is a variable that references the • <p> element with the ID selector, “todays-date” textContent is a property that represents the • content of an HTML element (in this case, <p>) • By assigning todaysDate to textContent , the <p> contents are modified ... Before script runs <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> After script runs <p id="todays-date">Monday, September 14, 2020</p> Slide 15 nc-national-parks-date.html

  16. JavaScript Built-in Functions • JavaScript has many built-in functions to perform frequently needed tasks, so that you do not have to write the code yourself • You can 'call' these functions and they will 'return' a value that you can assign to a variable in your script • JavaScript Date object has many methods to retrieve and format dates • w3schools: JavaScript Date Objects A new Date object is created that returns a string (or text) representation of the current date and time ... <p id="todays-date"></p> <script> let todaysDate = new Date(); let dateElement = document.querySelector("#todays-date"); dateElement.textContent = todaysDate; </script> </body> nc-national-parks-date-function.html Slide 16

  17. Formatting Dates 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 17

  18. Using JavaScript to Modify Content and Style Common scenarios • Provide error messages for invalid form inputs • Provide user feedback on long running tasks Slide 18

  19. Accessing DOM to Modify Style How to change the style of HTML elements <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> Change the specified CSS property for dateElement w3schools: HTML DOM Style Object Slide 19

  20. Quick exercise: • Add today's date to one of your web pages • Modify some of the style rules for the date Slide 20

Recommend


More recommend