DEV Community

Discussion on: innerHTML Vs. textContent: The subtle difference.

Collapse
 
chayimfriedman2 profile image
Chayim Friedman • Edited

innerHTML is a security hole.
(Along with not being a standard - it turns out that is is part of the HTML5 standard).

Collapse
 
giwajossy profile image
Giwa Jossy • Edited

Hmmm!
Cross-site scripting vulnerabilities.

I agree innerHTML is not completely safe in all use cases, it can still be used in some circumstances, such as inserting static data on a page where user input is not collected!

Curious, what would you use instead?

Collapse
 
chayimfriedman2 profile image
Chayim Friedman • Edited

Project Trusted Types, for example, prevents (not completely though, allows custom sanitizers) the use of innerHTML.

The alternative is very simple (although somewhat more verbose): document.createElement() and appendChild() etc..

Even better and faster are document fragments.