Winnie Pooh case... : )

This commit is contained in:
joaquinelio 2020-10-12 16:29:24 -03:00 committed by GitHub
parent ac289950ea
commit 54df94da3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -397,7 +397,7 @@ Compare the two:
<div id="elem2"></div> <div id="elem2"></div>
<script> <script>
let name = prompt("What's your name?", "<b>Winnie-the-pooh!</b>"); let name = prompt("What's your name?", "<b>Winnie-the-Pooh!</b>");
elem1.innerHTML = name; elem1.innerHTML = name;
elem2.textContent = name; elem2.textContent = name;
@ -405,7 +405,7 @@ Compare the two:
``` ```
1. The first `<div>` gets the name "as HTML": all tags become tags, so we see the bold name. 1. The first `<div>` gets the name "as HTML": all tags become tags, so we see the bold name.
2. The second `<div>` gets the name "as text", so we literally see `<b>Winnie-the-pooh!</b>`. 2. The second `<div>` gets the name "as text", so we literally see `<b>Winnie-the-Pooh!</b>`.
In most cases, we expect the text from a user, and want to treat it as text. We don't want unexpected HTML in our site. An assignment to `textContent` does exactly that. In most cases, we expect the text from a user, and want to treat it as text. We don't want unexpected HTML in our site. An assignment to `textContent` does exactly that.