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: 13_browser.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ HTML, which stands for _Hypertext Markup Language_, is the document format used
100
100
101
101
A short HTML document might look like this:
102
102
103
-
```{lang: "text/html"}
103
+
```{lang: "html"}
104
104
<!doctype html>
105
105
<html>
106
106
<head>
@@ -158,7 +158,7 @@ HTML is parsed in a remarkably error-tolerant way. When tags that should be ther
158
158
159
159
The following document will be treated just like the one shown previously:
160
160
161
-
```{lang: "text/html"}
161
+
```{lang: "html"}
162
162
<!doctype html>
163
163
164
164
<meta charset=utf-8>
@@ -188,7 +188,7 @@ I will also usually omit the ((doctype)) and `charset` declaration. This is not
188
188
189
189
In the context of this book, the most important HTML tag is `<script>`. This tag allows us to include a piece of JavaScript in a document.
190
190
191
-
```{lang: "text/html"}
191
+
```{lang: "html"}
192
192
<h1>Testing alert</h1>
193
193
<script>alert("hello!");</script>
194
194
```
@@ -201,7 +201,7 @@ Such a script will run as soon as its `<script>` tag is encountered while the br
201
201
202
202
Including large programs directly in HTML documents is often impractical. The `<script>` tag can be given an `src` attribute to fetch a script file (a text file containing a JavaScript program) from a URL.
203
203
204
-
```{lang: "text/html"}
204
+
```{lang: "html"}
205
205
<h1>Testing alert</h1>
206
206
<script src="code/hello.js"></script>
207
207
```
@@ -220,7 +220,7 @@ You can load ((ES modules)) (see [Chapter ?](modules#es)) in the browser by givi
220
220
221
221
Some attributes can also contain a JavaScript program. The `<button>` tag shown next (which shows up as a button) has an `onclick` attribute. The attribute's value will be run whenever the button is clicked.
222
222
223
-
```{lang: "text/html"}
223
+
```{lang: "html"}
224
224
<button onclick="alert('Boom!');">DO NOT PRESS</button>
Copy file name to clipboardExpand all lines: 14_dom.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ This representation of the ((document)) is one of the toys that a JavaScript pro
22
22
23
23
You can imagine an HTML document as a nested set of ((box))es. Tags such as `<body>` and `</body>` enclose other ((tag))s, which in turn contain other tags or ((text)). Here's the example document from the [previous chapter](browser):
24
24
25
-
```{lang: "text/html", sandbox: "homepage"}
25
+
```{lang: "html", sandbox: "homepage"}
26
26
<!doctype html>
27
27
<html>
28
28
<head>
@@ -176,7 +176,7 @@ All element nodes have a `getElementsByTagName` method, which collects all eleme
176
176
177
177
To find a specific _single_ node, you can give it an `id` attribute and use `document.getElementById` instead.
178
178
179
-
```{lang: "text/html"}
179
+
```{lang: "html"}
180
180
<p>My ostrich Gertrude:</p>
181
181
<p><img id="gertrude" src="img/ostrich.png"></p>
182
182
@@ -196,7 +196,7 @@ A third, similar method is `getElementsByClassName`, which, like `getElementsByT
196
196
197
197
Almost everything about the DOM data structure can be changed. The shape of the document tree can be modified by changing parent-child relationships. Nodes have a `remove` method to remove them from their current parent node. To add a child node to an element node, we can use `appendChild`, which puts it at the end of the list of children, or `insertBefore`, which inserts the node given as the first argument before the node given as the second argument.
198
198
199
-
```{lang: "text/html"}
199
+
```{lang: "html"}
200
200
<p>One</p>
201
201
<p>Two</p>
202
202
<p>Three</p>
@@ -223,7 +223,7 @@ Say we want to write a script that replaces all ((image))s (`<img>` tags) in the
223
223
224
224
This involves not only removing the images but adding a new text node to replace them. Text nodes are created with the `document.createTextNode` method.
225
225
226
-
```{lang: "text/html"}
226
+
```{lang: "html"}
227
227
<p>The <img src="img/cat.png" alt="Cat"> in the
228
228
<img src="img/hat.png" alt="Hat">.</p>
229
229
@@ -272,7 +272,7 @@ To create ((element)) nodes, you can use the `document.createElement` method. Th
272
272
273
273
The following example defines a utility `elt`, which creates an element node and treats the rest of its arguments as children to that node. This function is then used to add an attribution to a quote.
274
274
275
-
```{lang: "text/html"}
275
+
```{lang: "html"}
276
276
<blockquote id="quote">
277
277
No book can ever be finished. While working on it we learn
278
278
just enough to find it immature the moment we turn away
@@ -316,7 +316,7 @@ Some element ((attribute))s, such as `href` for links, can be accessed through a
316
316
317
317
But HTML allows you to set any attribute you want on nodes. This can be useful because it allows you to store extra information in a document. If you make up your own attribute names, though, such attributes will not be present as properties on the element's node. Instead, you have to use the `getAttribute` and `setAttribute` methods to work with them.
318
318
319
-
```{lang: "text/html"}
319
+
```{lang: "html"}
320
320
<p data-classified="secret">The launch code is 00000000.</p>
321
321
<p data-classified="unclassified">I have two feet.</p>
322
322
@@ -352,7 +352,7 @@ The size and position of an element can be accessed from JavaScript. The `offset
352
352
353
353
Similarly, `clientWidth` and `clientHeight` give you the size of the space _inside_ the element, ignoring border width.
354
354
355
-
```{lang: "text/html"}
355
+
```{lang: "html"}
356
356
<p style="border: 3px solid red">
357
357
I'm boxed in
358
358
</p>
@@ -386,7 +386,7 @@ Laying out a document can be quite a lot of work. In the interest of speed, brow
386
386
387
387
A program that repeatedly alternates between reading DOM layout information and changing the DOM forces a lot of layout computations to happen and will consequently run very slowly. The following code is an example of this. It contains two different programs that build up a line of _X_ characters 2,000 pixels wide and measures the time each one takes.
388
388
389
-
```{lang: "text/html", test: nonumbers}
389
+
```{lang: "html", test: nonumbers}
390
390
<p><span id="one"></span></p>
391
391
<p><span id="two"></span></p>
392
392
@@ -425,7 +425,7 @@ We have seen that different HTML elements are drawn differently. Some are displa
425
425
426
426
The way an `<img>` tag shows an image or an `<a>` tag causes a link to be followed when it is clicked is strongly tied to the element type. But we can change the styling associated with an element, such as the text color or underline. Here is an example that uses the `style` property:
@@ -446,7 +446,7 @@ A style attribute may contain one or more _((declaration))s_, which are a proper
446
446
447
447
A lot of aspects of the document can be influenced by styling. For example, the `display` property controls whether an element is displayed as a block or an inline element.
448
448
449
-
```{lang: "text/html"}
449
+
```{lang: "html"}
450
450
This text is displayed <strong>inline</strong>,
451
451
<strong style="display: block">as a block</strong>, and
452
452
<strong style="display: none">not at all</strong>.
@@ -466,7 +466,7 @@ if}}
466
466
467
467
JavaScript code can directly manipulate the style of an element through the element's `style` property. This property holds an object that has properties for all possible style properties. The values of these properties are strings, which we can write to in order to change a particular aspect of the element's style.
468
468
469
-
```{lang: "text/html"}
469
+
```{lang: "html"}
470
470
<p id="para" style="color: purple">
471
471
Nice text
472
472
</p>
@@ -491,7 +491,7 @@ Some style property names contain hyphens, such as `font-family`. Because such p
491
491
492
492
The styling system for HTML is called ((CSS)), for _Cascading Style Sheets_. A _style sheet_ is a set of rules for how to style elements in a document. It can be given inside a `<style>` tag.
493
493
494
-
```{lang: "text/html"}
494
+
```{lang: "html"}
495
495
<style>
496
496
strong {
497
497
font-style: italic;
@@ -550,7 +550,7 @@ The main reason I introduced _((selector))_ syntax—the notation used in style
550
550
551
551
The `querySelectorAll` method, which is defined both on the `document` object and on element nodes, takes a selector string and returns a `NodeList` containing all the elements that it matches.
552
552
553
-
```{lang: "text/html"}
553
+
```{lang: "html"}
554
554
<p>And if you go chasing
555
555
<span class="animal">rabbits</span></p>
556
556
<p>And you know you're going to fall</p>
@@ -593,7 +593,7 @@ The `position` style property influences layout in a powerful way. By default it
593
593
594
594
We can use this to create an animation. The following document displays a picture of a cat that moves around in an ((ellipse)):
Copy file name to clipboardExpand all lines: 15_event.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ Of course, it has to remember to look at the queue, and to do it often, because
28
28
29
29
A better mechanism is for the system to actively notify our code when an event occurs. Browsers do this by allowing us to register functions as _handlers_ for specific events.
30
30
31
-
```{lang: "text/html"}
31
+
```{lang: "html"}
32
32
<p>Click this document to activate the handler.</p>
33
33
<script>
34
34
window.addEventListener("click", () => {
@@ -47,7 +47,7 @@ The `window` binding refers to a built-in object provided by the browser. It rep
47
47
48
48
Each browser event handler is registered in a context. In the previous example we called `addEventListener` on the `window` object to register a handler for the whole window. Such a method can also be found on DOM elements and some other types of objects. Event listeners are called only when the event happens in the context of the object they are registered on.
49
49
50
-
```{lang: "text/html"}
50
+
```{lang: "html"}
51
51
<button>Click me</button>
52
52
<p>No handler here.</p>
53
53
<script>
@@ -72,7 +72,7 @@ But a node can have only one `onclick` attribute, so you can register only one h
72
72
73
73
The `removeEventListener` method, called with arguments similar to `addEventListener`, removes a handler.
74
74
75
-
```{lang: "text/html"}
75
+
```{lang: "html"}
76
76
<button>Act-once button</button>
77
77
<script>
78
78
let button = document.querySelector("button");
@@ -94,7 +94,7 @@ The function given to `removeEventListener` has to be the same function value th
94
94
95
95
Though we have ignored it so far, event handler functions are passed an argument: the _((event object))_. This object holds additional information about the event. For example, if we want to know _which_ ((mouse button)) was pressed, we can look at the event object's `button` property.
96
96
97
-
```{lang: "text/html"}
97
+
```{lang: "html"}
98
98
<button>Click me any way you want</button>
99
99
<script>
100
100
let button = document.querySelector("button");
@@ -136,7 +136,7 @@ At any point, an event handler can call the `stopPropagation` method on the even
136
136
137
137
The following example registers `"mousedown"` handlers on both a button and the paragraph around it. When clicked with the right mouse button, the handler for the button calls `stopPropagation`, which will prevent the handler on the paragraph from running. When the button is clicked with another ((mouse button)), both handlers will run.
138
138
139
-
```{lang: "text/html"}
139
+
```{lang: "html"}
140
140
<p>A paragraph with a <button>button</button>.</p>
141
141
<script>
142
142
let para = document.querySelector("p");
@@ -157,7 +157,7 @@ Most event objects have a `target` property that refers to the node where they o
157
157
158
158
It is also possible to use the `target` property to cast a wide net for a specific type of event. For example, if you have a node containing a long list of buttons, it may be more convenient to register a single click handler on the outer node and have it use the `target` property to figure out whether a button was clicked, rather than register individual handlers on all of the buttons.
159
159
160
-
```{lang: "text/html"}
160
+
```{lang: "html"}
161
161
<button>A</button>
162
162
<button>B</button>
163
163
<button>C</button>
@@ -184,7 +184,7 @@ For most types of events, the JavaScript event handlers are called _before_ the
184
184
185
185
This can be used to implement your own ((keyboard)) shortcuts or ((context menu)). It can also be used to obnoxiously interfere with the behavior that users expect. For example, here is a link that cannot be followed:
186
186
187
-
```{lang: "text/html"}
187
+
```{lang: "html"}
188
188
<a href="https://developer.mozilla.org/">MDN</a>
189
189
<script>
190
190
let link = document.querySelector("a");
@@ -207,7 +207,7 @@ Depending on the browser, some events can't be intercepted at all. On Chrome, fo
207
207
208
208
When a key on the keyboard is pressed, your browser fires a `"keydown"` event. When it is released, you get a `"keyup"` event.
209
209
210
-
```{lang: "text/html", focus: true}
210
+
```{lang: "html", focus: true}
211
211
<p>This page turns violet when you hold the V key.</p>
212
212
<script>
213
213
window.addEventListener("keydown", event => {
@@ -235,7 +235,7 @@ The example looked at the `key` property of the event object to see which key th
235
235
236
236
Modifier keys such as [shift]{keyname}, [control]{keyname}, [alt]{keyname}, and [meta]{keyname} ([command]{keyname} on Mac) generate key events just like normal keys. But when looking for key combinations, you can also find out whether these keys are held down by looking at the `shiftKey`, `ctrlKey`, `altKey`, and `metaKey` properties of keyboard and mouse events.
237
237
238
-
```{lang: "text/html", focus: true}
238
+
```{lang: "html", focus: true}
239
239
<p>Press Control-Space to continue.</p>
240
240
<script>
241
241
window.addEventListener("keydown", event => {
@@ -282,7 +282,7 @@ To get precise information about the place where a mouse event happened, you can
282
282
283
283
The following implements a primitive drawing program. Every time you click the document, it adds a dot under your mouse pointer. See [Chapter ?](paint) for a less primitive drawing program.
284
284
285
-
```{lang: "text/html"}
285
+
```{lang: "html"}
286
286
<style>
287
287
body {
288
288
height: 200px;
@@ -316,7 +316,7 @@ Every time the mouse pointer moves, a `"mousemove"` event is fired. This event c
316
316
317
317
As an example, the following program displays a bar and sets up event handlers so that dragging to the left or right on this bar makes it narrower or wider:
318
318
319
-
```{lang: "text/html", startCode: true}
319
+
```{lang: "html", startCode: true}
320
320
<p>Drag the bar to change its width:</p>
321
321
<div style="background: orange; width: 60px; height: 20px">
322
322
</div>
@@ -382,7 +382,7 @@ Because many touchscreens can detect multiple fingers at the same time, these ev
382
382
383
383
You could do something like this to show red circles around every touching finger:
384
384
385
-
```{lang: "text/html"}
385
+
```{lang: "html"}
386
386
<style>
387
387
dot { position: absolute; display: block;
388
388
border: 2px solid red; border-radius: 50px;
@@ -420,7 +420,7 @@ Whenever an element is scrolled, a `"scroll"` event is fired on it. This has var
420
420
421
421
The following example draws a ((progress bar)) above the document and updates it to fill up as you scroll down:
422
422
423
-
```{lang: "text/html"}
423
+
```{lang: "html"}
424
424
<style>
425
425
#progress {
426
426
border-bottom: 2px solid blue;
@@ -469,7 +469,7 @@ Unlike the events discussed earlier, these two events do not propagate. A handle
469
469
470
470
The following example displays help text for the ((text field)) that currently has focus:
471
471
472
-
```{lang: "text/html"}
472
+
```{lang: "html"}
473
473
<p>Name: <input type="text" data-help="Your full name"></p>
474
474
<p>Age: <input type="text" data-help="Your age in years"></p>
475
475
<p id="help"></p>
@@ -606,7 +606,7 @@ If you do need to do something nontrivial in such a handler, you can use `setTim
606
606
607
607
In the first example, we want to react when the user has typed something, but we don't want to do it immediately for every input event. When they are ((typing)) quickly, we just want to wait until a pause occurs. Instead of immediately performing an action in the event handler, we set a timeout. We also clear the previous timeout (if any) so that when events occur close together (closer than our timeout delay), the timeout from the previous event will be canceled.
608
608
609
-
```{lang: "text/html"}
609
+
```{lang: "html"}
610
610
<textarea>Type something here...</textarea>
611
611
<script>
612
612
let textarea = document.querySelector("textarea");
@@ -626,7 +626,7 @@ Giving an undefined value to `clearTimeout` or calling it on a timeout that has
626
626
627
627
We can use a slightly different pattern if we want to space responses so that they're separated by at least a certain length of ((time)) but want to fire them _during_ a series of events, not just afterward. For example, we might want to respond to `"mousemove"` events by showing the current coordinates of the mouse but only every 250 milliseconds.
628
628
629
-
```{lang: "text/html"}
629
+
```{lang: "html"}
630
630
<script>
631
631
let scheduled = null;
632
632
window.addEventListener("mousemove", event => {
@@ -672,7 +672,7 @@ When that works, add a feature where, if you blow up the balloon past a certain
672
672
673
673
{{if interactive
674
674
675
-
```{test: no, lang: "text/html", focus: yes}
675
+
```{test: no, lang: "html", focus: yes}
676
676
<p>🎈</p>
677
677
678
678
<script>
@@ -714,7 +714,7 @@ There are various possible approaches here. You can make your solution as simple
714
714
715
715
{{if interactive
716
716
717
-
```{lang: "text/html", test: no}
717
+
```{lang: "html", test: no}
718
718
<style>
719
719
.trail { /* className for the trail elements */
720
720
position: absolute;
@@ -764,7 +764,7 @@ When that works, extend it to style the button for the currently selected tab di
0 commit comments