html - execution of some code inside javascript function -
i have div example code in it. i'd execute code after pressing button. here's i've tried:
html
<button onclick="javascript:playcode()"> </button>
js
function playcode () { var execcode= ""; (i=1; i<=countcodeline (); i++) execcode += document.getelementbyid("line"+i).innertext; var div= document.getelementbyid ("mydiv"); var scr= document.createelement ('script', ""); div.innerhtml= "play() {"+execcode+"}"; div.appendchild(scr); play(); }
but doesn't work. how can this?
update: here errors console:
(line 9: pre.appendchild(scr)) uncaught syntaxerror: unexpected token { (line 10: play()) uncaught typeerror: object not function
if indeed getting right html execcode, can use
eval(execcode);
you can check doing simple console.log(execcode)
or alert(execcode)
verify it's loaded correctly.
however, can guarantee almost very, bad idea. it'd trivial edit dom , insert whatever malicious javascript want.
Comments
Post a Comment