Changing innerHTML content into createElement/Append - What is the best way to attack this?
I'll include a snippet of code I'd like to change from the security lacking innerHTML, to something more secure (assuming textContent once I've created the element + added a class to it), is anyone able to provide an example of how I would transfer this from innerHTML to a more secure variant?
The way I think it needs to be done is for each element, I need to create a variable that contains createElement, classList and the textContent, am I on the right path? I wuold then append each element to it's relevant parent, so for the below I THINK I create the div and append it to the nav, create the 3 a links and append them to the div.
5 Replies
Security-wise, there's nothing wrong with this
It's only a security issue once you add user generated content in there. So this would be an issue:
The way you'd do that with createElement and such:
It's very verbose
Oh I didn't know that about innerHTML, that's good to know!
perfect so you're just creating the element, adding a class, appending the element, so it is what i thought and I most definitely was ovethinking it all 😄
Thanks Jochem 🙂
np!
you can even set all the stuff on the A's with the same var:
Perfect!
This project suddenly went from very difficult, to quite simple 😂
nice!