When I try to load getContext function in javascript I have to use the window.load function to first load the DOM object before running my javascipt code. So my question is if there is any other way to run the getContext function without having to put all my code in a function,and without using other type a library. Here's is my code.
function init() {
var cvs = document.getElementById("myCanvas");
var ctx = cvs.getContext("2d");
ctx.rect(20,20,150,100);
ctx.stroke();
}
window.onload = init;