Effectively, your current code is not safe. innerHTML
doesn't run scripts in created <script>
elements, but it does run event handler content attributes.
function createDOM(str) {
document.createElement("div").innerHTML = str;
}
createDOM('<img src="//" onerror="console.log('You are pwned!')" />');
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…