forked from girldevelopit/gdi-featured-js-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass4.html
581 lines (508 loc) · 23.1 KB
/
class4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 4 - Intro to JavaScript - Girl Develop It</title>
<meta name="description" content="Girl Develop It framework for easily creating beautiful presentations using HTML in GDI theme. Forked from Hakim El Hattab's reveal.js">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/simple.css" id="theme">
<link rel="stylesheet" href="css/custom.css">
<!-- For syntax highlighting -->
<!-- light editor --> <link rel="stylesheet" href="lib/css/light.css">
<!-- dark editor <link rel="stylesheet" href="lib/css/dark.css">-->
<!-- <link rel="stylesheet" href="lib/css/zenburn.css">-->
<link rel="stylesheet" href="plugin/accessibility-helper/css/accessibility-helper.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="css/print/pdf.css" type="text/css" media="print">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<img src="img/circle-gdi-logo.png" alt="BDI Logo" class="noborder" style="max-height:400px;">
<h1>Introduction to JavaScript</h1>
<h2>Class 4</h2>
</section>
<!-- Review: Role of HTML v. CSS-->
<section>
<section>
<h3>Let's Review</h3>
<img src="img/elephants.jpg" alt="Two elephants interacting"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/ginable/325235488/">Ginable</a> <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">cc</a>
</section>
<section>
<h3>Anatomy of a website</h3>
<p><strong>Your Content</strong><br />
<strong>+ HTML</strong>: Structure<br />
<strong>+ CSS</strong>: Presentation<br />
<strong>= Your Website</strong></p>
<p>A website is a way to present your content to the world, using HTML and CSS to present that content & make it look good.</p>
</section>
<section>
<h3>HTML is Markup</h3>
<img src="img/markup.jpg" alt="Printed document covered in ink marks"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/nics_events/2349630241/">Nic's events</a> <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">cc</a>
</section>
<section>
<h3>CSS is Style</h3>
<img src="img/fingernails.jpg" alt="Fingernails with polka-dot paint job"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/pumpkincat210/4778025555/">pumpkincat210</a> <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">cc</a>
</section>
<section>
<h3>IDs vs. Classes</h3>
<p><strong>ID</strong> -- Should only apply to one element on a webpage, i.e., a webpage only has one footer.<br />
The "#" is how you tell CSS "this is an id."</p>
<p><strong>Class</strong> -- Lots of elements can have the same class, i.e., There can be many warnings on one webpage.<br />
The "." is how you tell CSS "this is a class name."</p>
</section>
</section>
<!--The DOM-->
<section>
<section>
<h3>Nesting</h3>
<p>HTML elements "nest" inside one another</p>
<img src="img/nesting-dolls.jpg" alt="Russian nesting dolls"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/johnkay/3539939004/">John K</a> <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">cc</a>
</section>
<section>
<h3>The DOM Tree</h3>
<p>We model the nested structure of an HTML page with the DOM (Document Object Model) Tree. The browser makes a "map" of all the elements on a page.</p>
</section>
<section>
<h3>The DOM: Sample Code</h3>
<pre><code class="html"><!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<style>
h1 {
color: red;
}
</style>
</head>
<body>
<h1>My Page</h1>
<p>Hello World!</p>
<img src="http://placekitten.com/g/200/300" alt="cat"/>
</body>
</html>
</code></pre>
</section>
<section>
<h3>The DOM: Sample Model</h3>
<img src="img/dom-tree.png" alt="Simple DOM Tree"/>
</section>
</section>
<!--Accessing the DOM-->
<section>
<section>
<h3>DOM Access</h3>
<p>Your browser automatically builds a Document object to store the DOM of a page. To change a page:</p>
<ol>
<li>Find the DOM node and store it in a variable</li>
<li>Use methods to manipulate the node</li>
</ol>
</section>
<section>
<h3>Finding Nodes</h3>
<img src="img/search.jpg" alt="A magnifying glass"/>
<p class="credit">Photo credit: <a href="https://flic.kr/p/4UokrH">Jeffery Beall</a> <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">cc</a>
</section>
<section>
<h3>DOM Access: By Id</h3>
<p>You can find nodes by id using the method:</p>
<pre><code class="js">document.getElementById(id);
</code></pre>
<p>To find:</p>
<pre><code class="html"><img id="kittenPic" src="http://placekitten.com/g/200/300" alt="cat"/>
</code></pre>
<p>We would use:</p>
<pre><code class="js">var imgKitten=document.getElementById('kittenPic');
</code></pre>
</section>
<section>
<h3>DOM Access: By Tag Name</h3>
<p>You can certain types of HTML elements using this method:</p>
<pre><code class="js">document.getElementsByTagName(tagName);
</code></pre>
<p>To find:</p>
<pre><code class="html"><li>Daisy</li>
<li>Tulip</li>
</code></pre>
<p>We would use:</p>
<pre><code class="js">var listItems=document.getElementsByTagName('li');
for (var i=0; i < listItems.length; i++) {
var listItem=listItems[i];
}
</code></pre>
</section>
<section>
<h3>DOM Access: HTML 5</h3>
<p>In newer browsers, you can use other methods too.</p>
<p>Available in <a href="http://caniuse.com/#search=getElementsByClassName">IE9+, FF3.6+, Chrome 17+, Safari 5+</a>:</p>
<pre><code class="js">document.getElementsByClassName(className);
</code></pre>
<p>Available in <a href="http://caniuse.com/#search=querySelectorAll">IE8+, FF3.6+, Chrome 17+, Safari 5+</a>:</p>
<pre><code class="js">document.querySelector(cssQuery);
document.querySelectorAll(cssQuery);
</code></pre>
</section>
<section>
<h3>getElement vs. getElements</h3>
<p>Any method that starts with "getElement" will return a <span class="blue">single</span> node.</p>
<pre><code class="js">document.getElementById('uniqueID'); //returns a single node
</code></pre>
<p>Any method that starts with "getElements" will return a <span class="blue">array</span> of nodes. To modify a single node, you will need to use bracket notation to select the correct one.</p>
<pre><code class="js">document.getElementsByTagName('p'); //returns multiple nodes
var specficParagraph=document.getElementsByTagName('p')[2];
</code></pre>
</section>
</section>
<!--Manipulating the DOM-->
<section>
<section>
<h3>Changing Nodes</h3>
<img src="img/two-color.jpg" alt="Red and yellow tulip"/>
<p class="credit">Photo credit: <a href="https://flic.kr/p/bCYp2S">Darryl Kenyon</a> <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/">cc</a>
</section>
<section>
<h3>DOM Nodes: Attributes</h3>
<p>You can access and change attributes of DOM nodes using dot notation.</p>
<p>To change this element:</p>
<pre><code class="html"><img id="kittenPic" src="http://placekitten.com/g/200/300" alt="cat"/>
</code></pre>
<p>We could change the src attribute this way:</p>
<pre><code class="js">var imgKitten=document.getElementById('kittenPic');
var oldSrc=imgKitten.src;
imgKitten.src='http://placekitten.com/g/600/500';
</code></pre>
</section>
<section>
<h3>DOM Nodes: Getting and Setting Attributes</h3>
<p>You can also use <code>getAttribute/setAttribute</code></p>
<pre><code class="html"><img id="kittenPic" src="http://placekitten.com/g/200/300" alt="cat"/>
</code></pre>
<p>We could change the src attribute this way:</p>
<pre><code class="js">var imgKitten=document.getElementById('kittenPic');
var oldSrc=imgKitten.getAttribute('src');
imgKitten.setAttribute('src', 'http://placekitten.com/g/600/500');
</code></pre>
</section>
<section>
<h3>DOM Nodes: Styles</h3>
<p>You can change page css using<code>style</code></p>
<p>To make this CSS:</p>
<pre><code class="css">body {
color: red;
}
</code></pre>
<p>Use this JavaScript:</p>
<pre><code class="js">var pageNode=document.getElementsByTagName('body')[0];
pageNode.style.color='red';
</code></pre>
</section>
<section>
<h3>DOM Nodes: More Styles</h3>
<p>Change any CSS property with a "-" to camelCase, and be sure to include a unit on any number</p>
<p>To make this CSS:</p>
<pre><code class="css">body {
background-color: pink;
padding-top: 10px;
}
</code></pre>
<p>Use this JavaScript:</p>
<pre><code class="js">var pageNode=document.getElementsByTagName('body')[0]
pageNode.style.backgroundColor='pink';
pageNode.style.paddingTop='10px';
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Create a simple HTML page or <a href="files/class4.zip">use this sample code</a>.</p>
<p>Isolate a node (an element on the page) and change an attribute or add a new style.</p>
</section>
</section>
<!--Manipulating the DOM-->
<section>
<section>
<h3>DOM innerHTML</h3>
<p>Each DOM node has an <code>innerHTML</code> property with the HTML of all its children. You can use the property to view or change the HTML of a node.</p>
<p>For example, you can overwrite the entire body:</p>
<pre><code class="js">var pageNode=document.getElementsByTagName('body')[0];
pageNode.innerHTML='<h1>Oh Noes!</h1>
<p>I just changed the whole page!</p>'
</code></pre>
<p>Or just add some new content to the end</p>
<pre><code class="js">pageNode.innerHTML +='...just adding this bit at the end of the page.';
</code></pre>
</section>
<section>
<h3>DOM innerHTML continued</h3>
<p>You can also target a particular element</p>
<p>To fill this HTML element:</p>
<pre><code class="html"><p id="warning"></p>
</code></pre>
<p>We can select the node and modify it</p>
<pre><code class="js">var warningParagraph=document.getElementById('warning');
warningParagraph.innerHTML='Danger Will Robinson!';
</code></pre>
</section>
<section>
<h3>Creating New Nodes</h3>
<p>The <code>document</code> object also provides ways to create nodes from scratch:</p>
<pre><code class="js">document.createElement(tagName);
document.createTextNode(text);
document.appendChild();
</code></pre>
</section>
<section>
<h3>Creating New Nodes: Sample Code</h3>
<pre><code class="js">var pageNode=document.getElementsByTagName('body')[0];
var newImg=document.createElement('img');
newImg.src='http://placekitten.com/g/500/200';
newImg.style.border='1px solid black';
pageNode.appendChild(newImg);
var newParagraph=document.createElement('p');
var paragraphText=document.createTextNode('Squee!');
newParagraph.appendChild(paragraphText);
pageNode.appendChild(newParagraph);
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Create a new paragraph element and add it to a div on your page.</p>
</section>
</section>
<!-- Events -->
<section id="events">
<section>
<h3>Events</h3>
<img src="img/magician.jpg" alt="Lego Magician" class="noborder"/>
<p class="credit">Photo credit: <a href="https://flic.kr/p/9hBrtv" target="_blank">Eva Peris</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Events</h3>
<p>An 'event' is a type of object that is created when the user interacts with a web page.</p>
<p>For example, JS creates an event when a user clicks an element.</p>
<pre><code class="js">element.onclick=function () {
//code to execute when the click happens
};
</code></pre>
</section>
<section>
<h3>Types of Events</h3>
<p>There are <a href="http://www.w3schools.com/jsref/dom_obj_event.asp">variety of events</a>. Some of the most common:</p>
<ul>
<li><strong>onclick:</strong> The event occurs when the user clicks on an element</li>
<li><strong>onmouseover:</strong> The event occurs when the pointer is moved onto an element</li>
<li><strong>onkeyup:</strong> The event occurs when the user releases a key</li>
<li><strong>onload:</strong> The event occurs when a document has been loaded</li>
<li><strong>onfocus:</strong> The event occurs when an element gets focus</li>
</ul>
</section>
<section>
<h3>Events and Code</h3>
<p>How do you make an event trigger some code?</p>
<ol>
<li>Break your code into functions</li>
<li>Associate a function with a JavaScript event</li>
</ol>
</section>
<section>
<h3>Calling Functions from HTML</h3>
<p>You can call a function directly from your HTML code:</p>
<pre><code class="html"><button id="clickMe" onclick="sayHi()">Click Me!</button>
</code></pre>
<pre><code class="js">function sayHi() {
alert ('Hi!');
}
</code></pre>
</section>
</section>
<!-- Listening Functions -->
<section>
<section>
<h3>Listening Functions</h3>
<img src="img/ear.jpg" alt="Ear"/>
<p class="credit">Photo credit: <a href="https://flic.kr/p/58bycz" target="_blank">Simon James</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Listening Functions</h3>
<p>Listening functions work like many of the other things we have done. First, find the object:</p>
<pre><code class="js">var myTarget=document.getElementById('clickMe');
</code></pre>
<p>Then add an event to that object:</p>
<pre><code class="js">myTarget.onclick=function(){
alert ('Hi!');
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Go back to the sample files you downloaded earlier.</p>
<p>Make some JavaScript code fire after an <code>onmouseover</code> event. Try adding the event to the HTML and adding a listening function.</p>
</section>
</section>
<!-- More Events -->
<section id="events2">
<section>
<h3>Preventing Defaults</h3>
<img src="img/donotwant.jpg" alt="A cat resisting a bath"/>
</section>
<section>
<h3>Preventing Defaults</h3>
<p>Elements like buttons and links have a default behaviors. However, the <code>event</code> objects has a built-in method to handle that:</p>
<pre><code class="js">element.onclick=function (event) {
event.preventDefault();
};
</code></pre>
</section>
<section>
<h3>This</h3>
<img src="img/arrow.jpg" alt="Lighted arrow sign pointed downward"/>
<p class="credit">Photo credit: <a href="https://flic.kr/p/6ei68A" target="_blank">Alan Berning</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>This</h3>
<p>The keyword <code>this</code> references the element that the user has just interacted with</p>
<pre><code class="js">element.onmouseover=function (event) {
console.log(this);
};
element.onclick=function (event) {
this.style.backgroundColor='red';
this.innerHTML='I've been clicked!';
};
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Write code that targets this link:</p>
<pre><code class="html"><a href="http://girldevelopit.com/" id="gdiLink">Girl Develop It</a>
</code></pre>
<p>When a user clicks the link, the page should display an error message instead of going to the Girl Develop It homepage.</p>
</section>
</section>
<!-- User input -->
<section id="input">
<section>
<h3>User Input</h3>
<img src="img/comment-box.jpg" alt="Comment box"/>
<p class="credit">Photo credit: <a href="https://flic.kr/p/fnZ4RT" target="_blank">Rym DeCoster</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Forms</h3>
<p>You can collect information from users to use in functions. The most common method is an HTML form</p>
<pre><code class="html"><form id="temperatureForm">
<label for="temp">Temperature:</label> <input type="text" id="temp" size="3"/> degrees in
<input type="radio" name="tempFormat" value="F" checked />Fahrenheit
<input type="radio" name="tempFormat" value="C" />Celsius <br />
<label for="submitButton"></label> <input id="tempSubmitButton" type="submit" value="Submit" />
</form>
</code></pre>
</section>
<section>
<h3>Retrieving Form Data</h3>
<p>You retrieve the values of form elements using the <code>value</code> method.</p>
<pre><code class="js">var temperature=document.getElementById('temp').value;
console.log (temperature);
</code></pre>
<p>You can retrieve the value of a form at any time. Try <code>onblur</code> (when a form element loses focus).</p>
</section>
<section>
<h3>Radio Buttons</h3>
<p>Radio buttons usually do not have IDs, so you will need to use an array:</p>
<pre><code class="js">var radios=document.getElementsByName('tempFormat');
for (var i=0, length=radios.length; i < length; i++) {
if (radios[i].checked) {
var radioButtonValue=radios[i].value;
// only one radio can be checked, so stop now
break;
}
}
</code></pre>
</section>
<section>
<h3>Submit buttons</h3>
<p>If you are going to retrieve form values with the submit button, be sure to prevent the default action!</p>
<pre><code class="js">var submitButton=document.getElementById('tempSubmitButton');
submitButton.onclick=function () {
event.preventDefault();
var temperature=document.getElementById('temp').value;
console.log (temperature);
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Collect a value from the input box on the page. Use it inside a function of some kind. For example, collect a number and multiply it by five or collect a name and display a greeting.</p>
</section>
</section>
<!-- Final slides-->
<section id="final">
<section>
<h3>You did it!</h3>
<img src="img/jumping-man.jpg" alt="Man jumping in air"/>
<p class="credit">Photo credit: <a href="http://www.flickr.com/photos/sunface13/988453859/" target="_blank">sunface13</a> <a href="http://creativecommons.org/licenses/by-nc/2.0/" target="_blank">cc</a></p>
</section>
<section>
<h3>Resources</h3>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide" target="_blank">JavaScript Guide</a>, from the Mozilla Developers Network.</li>
<li><a href="http://www.codecademy.com/tracks/javascript" target="_blank">Code Academy</a>, with interactive JavaScript lessons to help you review.</li>
<li><a href="https://www.khanacademy.org/computing/cs/programming">Khan Academy</a> has a lot more information abut drawing and animations.</li>
</ul>
</section>
<section>
<h2>Questions?</h2>
</section>
</section>
</div>
<footer>
<div class="copyright">
Intro to JavaScript -- Girl Develop It --
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/accessibility-helper/js/accessibility-helper.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>