After clearing document, document.write(ln) won't work?
as in title says I have a problem, here is example:
...
<script>
document.body.innerHTML = "";
document.write("<scr"+"ipt>alert(1);<\/scr"+"ipt>");
</script>
After clearing document, I want to write in it some JS code (and I want to be executed of course). I have tried other methods but it seems that they won't work (and I have browser Firef开发者_JAVA百科ox 6.0).
Does anyone know solution or working alternative for this problem? Thanks in advance!
Don't use document.write()
. Just don't. (See Why is document.write considered a "bad practice"?)
Try this:
var text = 'alert(1);',
script = document.createElement('script');
script.appendChild(document.createTextNode(text));
document.head.appendChild(script);
document.write only works before the DOM is loaded; document.body.innerHTML only works after.
Try using document.body.appendChild to append a new text node instead.
精彩评论