I want to use separate javascript file to dynamically add a tag into a HTML file, but always get an error: Cannot read property 'appendChild' of undefined. Really confused. Can anyone help ? Thank you so much! Below is my code in a .js file:
var canvasGraphic = (function(){
return {
getCanvas:function(){
var canvas = document.createElement("canvas");
var body = document.getElementsByTagName("body")[0];
body.appendChild(canvas);
canvas.style.border = "black solid";
}
}
})();
canvasGraphic.getCanvas();
<script src="..."></script>? Before or after<body>?