If you want the script to be interpreted, you need to add it to the document. Some browsers do not use innerHTML for scripts, but all will set the script text property.
<!doctype html>
<html lang= "en">
<head>
<meta charset= "utf-8">
<title> title</title>
<script>
window.onload=function(){
var head=document.getElementsByTagName('head')[0],
who= document.createElement('script');
who.text= 'alert("hey");';
head.appendChild(who);
}
</script>
</head>
<body>
</body>
</html>
source
share