run scripts with innerhtml code example

Example: execute js with innerhtml

<html>
<head>
 
<script>
function executer_script(monscript) {
    var head = document.getElementsByTagName("head")[0];
    var scr;
 
    var d = document.getElementById('resultat');
    d.innerHTML = monscript;
    var scrajx = d.getElementsByTagName('script');
 
    for(var i in scrajx) {
        scr = document.createElement("script");
        scr.type = "text/javascript";
        scr.text = scrajx[i].text;
        head.appendChild(scr);          
    }
}
</script>
 
</head>
<body>
 
<input type="button" value="test" onclick="executer_script('Mon html + du javascript <script>alert(\'test\');</script>');">
 
<div id="resultat"></div>
 
</body>
</html>

Tags:

Html Example