javascript - setting innerHTML with a script inside -
if run following line in firebug on page:
document.documentelement.innerhtml="<script>alert(1)</script>";
why isn't alert
command executed?
it looks <script>
tag is being added expect, code within not being executed. same failure happens if try using document.head
(or any other dom element, seems). whatever reason (possibly standards compliance, possible security), inline code inside of <script>
blocks added via .innerhtml
doesn't run.
however, have working code produces similar functionality:
var script = document.createelement('script'); script[(script.innertext===undefined?"textcontent":"innertext")] = 'alert(1);'; document.documentelement.appendchild(script);
here, add <script>
block documentelement.appendchild
, use textcontent
or innertext
set content of <script>
.
Comments
Post a Comment