script in innerhtml code example

Example 1: .innerhtml

// .innerHTML method is used to change the html contents of a DOM object
document.getElementById("demo").innerHTML = "Paragraph changed!";

Example 2: innerHTML

document.getElementById("Test").innerHTML = "<h1>Test</h1>";

Example 3: 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