This approach worked for me:
<body>
<div id="main">
</div>
<script>
// create a script element
var script = document.createElement('script');
// fill its inner html with js code
script.innerHTML = 'alert("Javascript");'
// add it inside your target div and then profit!
document.getElementById('main').appendChild(script);
</script>
</body>
Edit:
I've found more info about your problem here, I suggest you read the question, it has plenty of helpful answers and it also explains why your first approach did not work: Can scripts be inserted with innerHTML?
A simple vanilla approach of using this code to write data inside a div after the page has loaded could be done like this:
<html>
<head>
<script>
window.onload = function () {
var script = document.createElement('script');
script.innerHTML = 'alert("Javascript");'
document.getElementById('main').appendChild(script);
}
</script>
</head>
<body>
<div id="main">
</div>
</body>
</html>