-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·479 lines (311 loc) · 11.2 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>DOM & BOM</title>
<meta charset="utf-8"/>
<link href="css/reset.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
html, body {
font-family: 'Roboto', sans-serif;
background: #fafafa;
}
table {
display: block;
width: 100%;
overflow: auto;
word-break: normal;
word-break: keep-all
}
table th {
font-weight: bold
}
table th, table td {
padding: 6px 13px;
border: 1px solid #ddd
}
table tr {
background-color: #fff;
border-top: 1px solid #ccc
}
table tr:nth-child(2n) {
background-color: #f8f8f8
}
.center > table{
margin: 0 auto;
width: auto;
display: table;
}
td > strong {
color: red;
}
.hljs-github .hljs {
background: #f0f0f0;
}
.hljs-github .hljs-tag, .hljs-github .hljs-name, .hljs-github .hljs-attribute {
color: #0000ff;
}
.hljs-github .hljs-string, .hljs-github .hljs-doctag {
color: #ff4e00;
}
.hljs-github .hljs {
color: #808080;
}
</style>
</head>
<body>
<div class="wrapper">
<header class="mainheader">
<div class="logo">
<div class="divider"><a href="../index.html"><img src="Logo.png"></a>
</div>
<a href="#"><span class="logotext">CURSOR EDUCATION - JS Essentials Lecture</span></a>
</div>
</header>
</div>
<!-- ================== Slides ================== -->
<textarea id="source">
class: center, middle
.title[
Front-end training
# DOM & BOM
]
---
# DOM, BOM and JS
![Alt text](assets/DOM1.png)
---
# What is the DOM?
The DOM is a W3C (World Wide Web Consortium) standard.
The DOM defines a standard for accessing documents:<br/>
_"The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows
programs and scripts to dynamically access and update the content, structure, and style of a
document."_
The W3C DOM standard is separated into 3 different parts:
- Core DOM - standard model for all document types
- XML DOM - standard model for XML documents
- HTML DOM - standard model for HTML documents
---
# What is the HTML DOM?
The HTML DOM is a standard object model and programming interface for HTML. It defines:
- The HTML elements as objects
- The properties of all HTML elements
- The methods to access all HTML elements
- The events for all HTML elements
In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML
elements.
---
# DOM
![Alt text](assets/DOM2.png)
---
# DOM
With the DOM, JavaScript gets all the power it needs to create dynamic HTML:
- JavaScript can change all the HTML elements in the page
- JavaScript can change all the HTML attributes in the page
- JavaScript can change all the CSS styles in the page
- JavaScript can remove existing HTML elements and attributes
- JavaScript can add new HTML elements and attributes
- JavaScript can react to all existing HTML events in the page
- JavaScript can create new HTML events in the page
---
# DOM
Reaching Elements in a Document
```javascript
//Retrieves the element with the given id as an object
document.getElementById('id');
//Retrieves all elements with the tag name tagname and stores them in an
//array like
document.getElementsByTagName('tagname');
```
Reading Element Attributes, Node Values and Other Data
```javascript
//Retrieves the value of the attribute with the name attribute
node.getAttribute('attribute');
//Sets the value of the attribute with the name attribute to value
node.setAttribute('attribute', 'value');
//Reads the type of the node (1 = element, 3 = text node)
node.nodeType;
//Reads the name of the node (either element name or #textNode)
node.nodeName;
//Reads or sets the value of the node (the text content in the case of
//text nodes)
node.nodeValue;
```
---
# DOM
Navigating Between Nodes
```javascript
//Retrieves the previous sibling node and stores it as an object.
node.previousSibling;
//Retrieves the next sibling node and stores it as an object.
node.nextSibling;
//Retrieves all child nodes of the object and stores them in an list.
//here are shortcuts for the first and last child node, named node.
//firstChild and node.lastChild.
node.childNodes;
//Retrieves the node containing node.
node.parentNode;
```
---
# DOM
Creating New Nodes
```javascript
//Creates a new element node with the name element. You provide the name
//as a string.
document.createElement(element);
//Creates a new text node with the node value of string.
document.createTextNode(string);
//Creates newNode as a copy (clone) of node. If bool is true, the clone
//includes clones of all the child nodes of the original.
newNode = node.cloneNode(bool);
//Adds newNode as a new (last) child node to node.
node.appendChild(newNode);
//Inserts newNode as a new child node of node before oldNode.
node.insertBefore(newNode,oldNode);
//Removes the child oldNode from node.
node.removeChild(oldNode);
//Replaces the child node oldNode of node with newNode.
node.replaceChild(newNode, oldNode);
//Reads or writes the HTML content of the given element as a string—
//including all child nodes with their attributes and
//text content.
element.innerHTML;
```
---
# Mouse Events
***onclick*** <br/>The event occurs when the user clicks on an element
***ondblclick*** <br/>The event occurs when the user double-clicks on an element
***onmousedown*** <br/>The event occurs when a user presses a mouse button over an element
***onmousemove*** <br/>The event occurs when the pointer is moving while it is over an element
***onmouseover*** <br/>The event occurs when the pointer is moved onto an element
***onmouseout*** <br/>The event occurs when a user moves the mouse pointer out of an element
***onmouseup*** <br/>The event occurs when a user releases a mouse button over an element
---
# Keyboard Events
***onkeydown***
<br/>The event occurs when the user is pressing a key
***onkeypress***
<br/>The event occurs when the user presses a key
***onkeyup***
<br/>The event occurs when the user releases a key
---
# Handlers
Inline
``` html
<div onclick='getACupcake(event)'>
```
addEventListener()
```javascript
document.getElementById('cupcakeButton').addEventListener('click', getACupcake);
document.getElementById('cupcakeButton').attachEvent('onclick', getACupcake);
//Note: Internet Explorer 9 adds support for addEventListener().
```
Assigning to event handler properties
```javascript
document.getElementById('cupcakeButton').onclick = getACupcake;
```
---
# Event object
event.target is the original element the event happened to. However, Internet Explorer calls it event.
srcElement. In fact, Chrome and Safari started setting both. You can do this for maximum
compatibility:
```javascript
function getACupcake(event) {
var target = event.target || event.srcElement;
target.style.backgroundColor = '#F00'; // use it
}
```
---
# Delayed Execution
Time of code execution can be changed with ["WindowTimers"](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers)
`var timer = setTimeout(fn, ms);` execute `fn` after `ms` milliseconds.
`typeof timer; // "number"` setTimeout returns an identificator
`clearTimeout(timer);` prevents execution of early setted timeout.
`var interval = setInterval(fn, ms);` execute `fn` after every `ms` (multiple executions).
`clearInterval(interval);` stop interval execution.
Execute `fn` after 1/60 of second. Useful for animations.
```javascript
var animFrame = requestAnimationFrame(fn);
cancelAnimationFrame(animFrame);
```
---
# Location
[Location](https://developer.mozilla.org/en-US/docs/Web/API/Location) object used to change URL in browser.
```javascript
location.href; // get full URL
location.protocol; // get protocol
location.host; // get host, w/o url
location.pathname; // get pathname, w/o hostname
location.port; // get port
location.hash; // get hash (#)
location.search; // get search (?value=...)
// you may set each of this properties, like
location.href = 'http://google.com'; // redirect user browser to google.com
location.hash = '#11';
// this event fire when hash was changed
window.addEventListener('hashchange', function(e) {
console.log(e.oldURL + ' -> ' + e.newURL);
});
```
---
# Encoding/deconding
Since some characters (like, /, ?, #, :, =, etc) are reserved for using as URL delimiters,
as well as non-ascii characters (non-latin alphabet), you need to encode values in order
to get correct result. Browsers use escape-sequences, containing '%xx', where xx is a symbol code.
Up to 3 '%xx%yy%zz' may be used to encode some wide characters (like japanese symbols).
```javascript
// Encode only non-latin characters, leave all url-specific symbols in place
encodeURI(value);
// Encode all not-allowed characters, including /, ?, ...
encodeURIComponent(value);
// Decode only non-latin characters
decodeURI(value);
// Decode all delimiter characters
decodeURIComponent(value);
```
[Article Explaining URL and encodings](http://www.skorks.com/2010/05/what-every-developer-should-know-about-urls/)
---
# Navigator
[Navigator](https://developer.mozilla.org/en-US/docs/Web/API/Navigator) object is used to detect client browser type.
Since earlier many sites support only Netscape - all browsers use _'Mozilla'_ as
values for `appName`. This, actually, is written in standard, so this property is not
very useful.
```javascript
// get user-agent string, which can be parsed and detect browser.
navigator.userAgent;
// Array of installed plugins. May be used to detect installed plugins, like Adobe Flash
navigator.plugins;
// Current system language
navigator.language;
// If user allows you to track his position you may use geolocation to retrieve
// user current coordinates (HTML5)
navigator.geolocation.getCurrentPosition(success, error);
var positionChangeWatch = navigator.geolocation.watchPosition(success, error);
navigator.geolocation.clearWatch(positionChangeWatch);
```
---
# Local/Session storage
Data is stored as simple key-value pairs, both strings.
[localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) saves values forever, while [sessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) - only until browser closed.
`localStorage.setItem(key, value);` store value in storage
`localStorage.getItem(key);` get value from storage. If value does not exist `null` is returned.
`localStorage.removeItem(key);` remove key-value pair from storage.
`localStorage.clear();` remove all values from storage.
Same methods available for `sessionStorage`.
---
# Dimensions
`window.innerWidth` get page working area width.
`window.innerHeight` get page working area height.
`screen` [Screen](https://developer.mozilla.org/en-US/docs/Web/API/Screen) object with dimensions, orientation, pixelDepth, etc.
---
class: center, middle
.title[
## Thanks
## The End
]
</textarea>
<!-- ================== End of Slides ================== -->
<script src="js/remark-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">remark.create({highlightStyle: 'github'});</script>
</body>
</html>