You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/1-document/01-browser-environment/article.md
+24-23
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,21 @@
1
1
# Browser environment, specs
2
2
3
-
The JavaScript language was initially created for web browsers. But as of now, it evolved and became a language with many uses and platforms.
3
+
The JavaScript language was initially created for web browsers. Since then, it evolved and became a language with many uses and platforms.
4
4
5
-
A platform may be either a browser or a web-server or a washing machine or another *host*. Each of them provides platform-specific functionality. The JavaScript standard called that a *host environment*.
5
+
A platform may be either a browser or a web-server or a washing machine or another *host*. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
6
6
7
-
That host environment provides additional objects and functions to the language core. Web browsers provide means to control web pages. Node.JS provides server-side features. There are other host environments too.
7
+
A host environment provides platform-specific objects and functions additionally to the language core. Web browsers give means to control web pages. Node.JS provides server-side features, and so on.
8
8
9
9
[cut]
10
10
11
11
Here's a bird-eye view of what we have when JavaScript runs in a web-browser:
12
12
13
13

14
14
15
-
The "root object" called `window` has two roles:
15
+
There's a "root" object called `window`. It has two roles:
16
16
17
-
1. First, it is a [global object](info:global-object) for JavaScript code.
18
-
2. Second, it represents the "browser window" object, provides methods to control it.
17
+
1. First, it is a global object for JavaScript code, as described in the chapter <info:global-object>.
18
+
2. Second, it represents the "browser window" and provides methods to control it.
19
19
20
20
For instance, here we use it as a global object:
21
21
@@ -24,18 +24,21 @@ function sayHi() {
24
24
alert("Hello");
25
25
}
26
26
27
-
alert(window.sayHi); // global function is a property of window
27
+
// global functions are accessible as properties of window
28
+
alert(window.sayHi);
28
29
```
29
30
30
31
And here we use it as a browser window, to see the window height:
31
32
32
33
```js run
33
-
alert(window.innerHeight); //some number
34
+
alert(window.innerHeight); //inner window height
34
35
```
35
36
37
+
There are more window-specific methods and properties, we'll cover them later.
38
+
36
39
## Document Object Model (DOM)
37
40
38
-
The `document` object gives access to the page content. We can change or create literally anything.
41
+
The `document` object gives access to the page content. We can change or create anything on the page using it.
Here we used `document.body.style`, but there's much, much more. Properties and methods are described in the specification.
50
-
51
-
There are two working groups who develop it:
52
+
Here we used `document.body.style`, but there's much, much more. Properties and methods are described in the specification. By chance, there are two working groups who develop it:
52
53
53
54
1.[W3C](https://en.wikipedia.org/wiki/World_Wide_Web_Consortium) -- the documentation is at <https://www.w3.org/TR/dom>.
54
55
2.[WhatWG](https://en.wikipedia.org/wiki/WHATWG), publishing at <https://dom.spec.whatwg.org>.
55
56
56
-
As it happens, the two groups don't always agree, so we have like 2 sets of standards. But they are in tight contact and eventually things merge. So the documentation that you can find on the given resources is very similar, like 99%. There are very minor differences, but you probably won't notice them.
57
+
As it happens, the two groups don't always agree, so we have like 2 sets of standards. But they are in the tight contact and eventually things merge. So the documentation that you can find on the given resources is very similar, there's like 99% match. There are very minor differences, you probably won't notice them.
57
58
58
-
I find <https://dom.spec.whatwg.org> more pleasant to use, and so recommend it.
59
+
Personally, I find <https://dom.spec.whatwg.org> more pleasant to use.
59
60
60
-
In the ancient past, once there was no standard at all -- each browser did whatever it wanted. So different browsers had different sets methods and properties for the same thing, and developers had to write different code for each of them. Dark times indeed.
61
+
In the ancient past, there was no standard at all -- each browser implemented whatever it wanted. So different browsers had different sets methods and properties for the same thing, and developers had to write different code for each of them. Dark, messy times.
61
62
62
63
Even now we can sometimes meet old code that uses browser-specific properties and works around incompatibilities. But in this tutorial we'll use modern stuff: there's no need to learn old things until you really need those (chances are high you won't).
63
64
64
-
Then the DOM standard appeared, in an attempt to bring everyone to an agreement. The first version was DOM Level 1, then it was extended by DOM Level 2, then DOM Level 3, and now it's DOM Level 4.
65
-
66
-
People from WhatWG group got tired of version and are calling that just "DOM", without a number. So will do we.
65
+
Then the DOM standard appeared, in an attempt to bring everyone to an agreement. The first version was "DOM Level 1", then it was extended by DOM Level 2, then DOM Level 3, and now it's DOM Level 4. People from WhatWG group got tired of version and are calling that just "DOM", without a number. So will do we.
67
66
68
67
```smart header="DOM is not only for browsers"
69
68
The DOM specification explains the structure of a document and provides objects to manipulate it. There are non-browser instruments that use it too.
@@ -72,9 +71,9 @@ For instance, server-side tools that download HTML pages and process them. They
72
71
```
73
72
74
73
```smart header="CSSOM for styling"
75
-
CSS styles and stylesheets are structured not like HTML. So there's a separate specification [CSSOM](https://www.w3.org/TR/cssom-1/) that explains how CSS styles and rules can be represented as objects, how to read and write them.
74
+
CSS rules and stylesheets are structured not like HTML. So there's a separate specification [CSSOM](https://www.w3.org/TR/cssom-1/) that explains how they are represented as objects, how to read and write them.
76
75
77
-
Usually we take a style somewhere from the document and apply it to the document, so CSSOM is used together with DOM. But CSSOM is applied not often, because we rarely need to modify CSS rules from JavaScript, so we won't cover it right now.
76
+
CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because usually CSS rules are static. We rarely need to add/remove CSS rules from JavaScript, so we won't cover it right now.
78
77
```
79
78
80
79
## BOM (part of HTML spec)
@@ -95,7 +94,7 @@ if (confirm("Go to wikipedia?")) {
95
94
}
96
95
```
97
96
98
-
Functions `alert/confirm/prompt`-- are also a part of BOM: they are directly not related to the document, but represent pure browser methods of communicating with the user.
97
+
Functions `alert/confirm/prompt` are also a part of BOM: they are directly not related to the document, but represent pure browser methods of communicating with the user.
99
98
100
99
101
100
```smart header="HTML specification"
@@ -112,11 +111,13 @@ DOM specification
112
111
: Describes the document structure, manipulations and events, see <https://dom.spec.whatwg.org>.
113
112
114
113
CSSOM specification
115
-
: Describes styles, manipulations with them and their binding to documents, see <https://www.w3.org/TR/cssom-1/>.
114
+
: Describes stylesheets and style rules, manipulations with them and their binding to documents, see <https://www.w3.org/TR/cssom-1/>.
116
115
117
116
HTML specification
118
117
: Describes HTML language (tags etc) and also BOM (browser object model) -- various browser functions: `setTimeout`, `alert`, `location` and so on, see <https://html.spec.whatwg.org>. It takes DOM specification and extends it with many additional properties and methods.
119
118
120
119
Now we'll get down to learning DOM, because the document plays the central role in the UI, and working with it is the most complex part.
121
120
122
-
Please note the links above, because there's so many stuff to learn, it's impossible to cover and remember everything. When you'd like to read about a property or a method -- the Mozilla manual at <https://developer.mozilla.org/en-US/search> is a nice one, but reading the corresponding spec may be better. More complex and longer to read, but will make your fundamental knowledge sound and complete.
121
+
Please note the links above, because there's so many stuff to learn, it's impossible to cover and remember everything.
122
+
123
+
When you'd like to read about a property or a method -- the Mozilla manual at <https://developer.mozilla.org/en-US/search> is a nice one, but reading the corresponding spec may be better: more complex and longer to read, but will make your fundamental knowledge sound and complete.
Copy file name to clipboardExpand all lines: 2-ui/1-document/02-dom-nodes/article.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,13 @@ libs:
6
6
7
7
# DOM tree
8
8
9
-
The essential part of HTML is tags, right?
9
+
The backbone of an HTML document is tags.
10
10
11
-
According to Document Object Model (DOM), every HTML-tag is an object. Nested tags are called "children". And the text inside it is an object as well. All these objects are accessible using JavaScript, we'll see that now.
11
+
According to Document Object Model (DOM), every HTML-tag is an object. Nested tags are called "children" of the enclosing one.
12
+
13
+
The text inside a tag it is an object as well.
14
+
15
+
All these objects are accessible using JavaScript.
In the task <info:task/animate-circle> an animated growing circle is shown.
5
+
6
+
Now let's say we need not just a circle, but to show a message inside it. The message should appear *after* the animation is complete (the circle is fully grown), otherwise it would look ugly.
7
+
8
+
In the solution of the task, the function `showCircle(cx, cy, radius)` draws the circle, but gives no way to track when it's ready.
9
+
10
+
Add a callback argument: `showCircle(cx, cy, radius, callback)` to be called when the animation is complete. The `callback` should receive the circle `<div>` as an argument.
11
+
12
+
Here's the example:
13
+
14
+
```js
15
+
showCircle(150, 150, 100, div=> {
16
+
div.classList.add('message-ball');
17
+
div.append("Hello, world!");
18
+
});
19
+
```
20
+
21
+
Demo:
22
+
23
+
[iframe src="solution" height=260]
24
+
25
+
Take the solution of the task <info:task/animate-circle> as the base.
0 commit comments