Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript Basics Three evening classes A, B and C

Similar presentations


Presentation on theme: "JavaScript Basics Three evening classes A, B and C"— Presentation transcript:

1 JavaScript Basics Three evening classes A, B and C
Version 2.4 June 23, 2018

2 HTML Nomenclature: <body> . . . </body> or
Elements, Tags, Attributes and Attribute Values A Text file with extension .htm or .html Composed of HTML elements, like <body> </body> or <p> </p>

3 HTML Nomenclature: Elements
Most elements (about 100) have an opening tag <body> and a closing tag </body> A few elements (about 10) have no closing tags Examples: <img> (image) <br> (break) <hr> (horizontal rule) <meta> ("Meta" information)

4 HTML Nomenclature: Elements
“Legal" web page must follow some rules. Examples: One and only one <html> element. One and only one <head> element. One and only one <body> element. One and only one <title> element.

5 HTML Nomenclature: Elements
“Legal" web page must follow some rules. Examples: One and only one <html> element. One and only one <head> element. One and only one <body> element. One and only one <title> element. The <html> element contains just 2 elements, <head> and <body>. The <title> element always inside the <head> element.

6 HTML Nomenclature: Elements
Opening tag of an HTML element can have attributes and attribute values. <body class="artistic" onLoad="init()"> attribute attribute

7 HTML Nomenclature: Elements
Opening tag of an HTML element can have attributes and attribute values. <body class="artistic" onLoad="init()"> Attribute Value Attribute Value

8 HTML Nomenclature: Elements
Opening tag of an HTML element can have attributes and attribute values. <body class="artistic" onLoad="init()"> An HTML element attribute is separated from its attribute value with an equal sign.

9 HTML Nomenclature: Elements
<body class="artistic" onLoad="init()"> HTML attribute names should be in Camel case (convention) Attribute values should be in either single or double quotes HTML element names and attribute names are NEVER in quotes.

10 CSS – Cascading Style Sheet
Web pages optionally use CSS. 99.99% of all web pages use CSS.

11 CSS Syntax body { background-color: blue; color: white; } p {
background-color: red; color: blue;

12 CSS Nomenclature: Selector, Property, Property Value selector selector
body { background-color: blue; color: white; } p { background-color: red; color: blue; selector

13 CSS Nomenclature: Selector, Property, Property Value properties body {
background-color : blue; color : white; } p { background-color : red; color :blue; properties

14 CSS Nomenclature: Selector, Property, Property Value property values
body { background-color: blue ; color: white ; } p { background-color: red; color: blue ; property values

15 Three Ways to place CSS in Web Page:
1. Embedded <style> element in <head> <head> . . . <style> body { background-color: blue; color: white; } </style> </head>

16 Three Ways to place CSS in Web Page:
2. External .css file with <link> element my.html <head> . . . <link rel="stylesheet" href="my.css"> </head> my.css body { background-color: blue; color: white; }

17 Three Ways to put CSS in a Web Page:
3. In-line css with a style attribute: <body style="background-color:blue; color:white">

18 Three Ways to put CSS in a Web Page:
3. In-line css with a style attribute: <body style="background-color:blue; color:white">

19 Problem 1: from downloaded js1.html on
Create web page js1-embedded-css.html from downloaded js1.html on scottsdevelopers.com | Resources | Intro to JavaScript | Class B Add embedded CSS with a <style> element that Changes webpage background color to blue Changes the paragraph text color to yellow

20 Problem 2: Use Output should be identical to Problem 1.
Create web page js1-external-css.html and js1-external-css.css from js1.html Use <link rel="stylesheet" href="js1-external-css.css"> Output should be identical to Problem 1.

21 Problem 3: Create web page js1-inline-css.html from js1.html Use <body style="background-color:blue;"> <p style="color:yellow;"> Output should be identical to Problem 1.

22 Variables, Data Types, Primitive Data Types, Functions, Objects
JavaScript Nomenclature: Variables, Data Types, Primitive Data Types, Functions, Objects

23 JavaScript Nomenclature: Variables
variables can be primitive or non-primitive.

24 Non-Primitive Data Types:
Number Character String Boolean Non-Primitive Data Types: Objects .

25 var numStudents = 23; (Number) var name="Scott"; (String)
Example Primitive Data Types: var numStudents = 23; (Number) var name="Scott"; (String) var isHappy = true; (Boolean) .

26 var car = { year:1970, make:'BMW', color:'silver', isDiesel: true }
Example Object: var car = { year:1970, make:'BMW', color:'silver', isDiesel: true }

27 var car = { year:1970, make:'BMW', color:'silver', isDiesel: true }
Example Object: var car = { year:1970, make:'BMW', color:'silver', isDiesel: true } variable

28 var car = { year:1970, make:'BMW', color:'silver', isDiesel: true }
Example Object: var car = { year:1970, make:'BMW', color:'silver', isDiesel: true } Object

29 var car = { year:1970, make:'BMW', color:'silver', isDiesel: true }
Example Object: var car = { year:1970, make:'BMW', color:'silver', isDiesel: true } Properties

30 Example Property: year:1970 Name:Value Pair

31 var hotel = { chain:'Best Western', name:'The Retreat', numRooms: 24,
Another Example Object: var hotel = { chain:'Best Western', name:'The Retreat', numRooms: 24, hasPool: false }

32 Primitive Data Types: Object Data Types
Built into the JavaScript Language. Object Data Types Created by Programmers to represent the real world.

33 Property values of an Object accessed with “Dot” notation Example:
alert("hotel name is " + hotel.name); "

34 Problem 4: Store Hotel Data in two Hotel Objects Step 1
Download to Atom and Desktop js5.html from scottsdevelopers.com | Resources | Intro to JavaScript | Class B

35 Problem 4: Compare web page and source

36 Convert this data into Objects
Problem 4: Convert this data into Objects

37 Problem 4: Store Hotel Data in two Hotel Objects

38 Problem 4: Store Hotel Data in two Hotel Objects Step 2
Download to Atom and Desktop js5.js from scottsdevelopers.com | Resources | Intro to JavaScript | Class B

39 Problem 4: Compare data in web page source HTML content with equivalent JavaScript Objects:

40 Problem 4: Information is content text in HTML elements

41 Problem 4: Information is in JavaScript Object variables

42 Problem 4: Step 3 Create an external <script> to the
JavaScript file js5.js with <script src="js5.js"></script> in the <head>

43 Problem 4: Step 3 . . . <style> </style>
<head> . . . <style> </style> <script src="js5.js"></script> </head>

44 Problem 4: Step 4 Change pure HTML lines: to
<p>Chain: Best Western </p> to HTML and JavaScript lines: <p>Chain: hotel1.chain </p>

45 Problem 4: Step 4

46 Problem 4: Step 4

47 Problem 4: Step 4 Want to see it again?

48 Problem 4: Step 4

49 Problem 4: Step 4

50 Problem 4: Step 5 Look at web page:

51 Problem 4: Step 5 Look at web page:

52 You can’t mix HTML

53 With JavaScript

54 How to write JavaScript in HTML?

55 This is where we want to write JavaScript:

56 Need to insert a <script> element:

57 How about this?:

58 Nope.

59 Time for a new JavaScript function! document.write("Hello, World!");

60 Time for a new JavaScript function!
document.write("Hello, World!"); Writes directly to the web page from JavaScript.

61 This is the JavaScript to write out the hotel1
This is the JavaScript to write out the hotel1.chain variable to the screen: <script> document.write(hotel1.chain); </script>

62 Complete Solution: <p>Chain: <script>document.write(hotel1.chain);</script></p>

63 Problem 4: <h2> Hotel 1 </h2>
<p>Chain: <script> document.write(hotel1.chain) </script> </p> <p>Name: <script> document.write(hotel1.name) </script> </p> <p>Number of Rooms: <script> document.write(hotel1.numRooms) </script> </p> <p>Has Pool?: <script> document.write(hotel1.hasPool) </script> </p> <h2> Hotel 2 </h2> <p>Chain: <script> document.write(hotel2.chain) </script> </p> <p>Name: <script> document.write(hotel2.name) </script> </p> <p>Number of Rooms: <script> document.write(hotel2.numRooms) </script> </p> <p>Has Pool?: <script> document.write(hotel2.hasPool) </script> </p>

64 Problem 4:

65 Homework: Update js5.html with a <button> which removes two rooms from both hotels and removes the pool from the Radisson.

66


Download ppt "JavaScript Basics Three evening classes A, B and C"

Similar presentations


Ads by Google