Javascript document.getElementById("name") returning -


this question has answer here:

i've searched answer question , haven't been able find hits it, far saw.

i have 2 chunks of javascript here using create simple 2d game in web browser.

var mygamepiece;  var game = {      canvas : document.getelementbyid("game-screen"),      start : function(){         this.context = this.canvas.getcontext("2d");     } }  function startgame(){     game.start(); } 

and one:

var mygamepiece;  var game = {      start : function(){          this.canvas = document.getelementbyid("game-screen");         this.context = this.canvas.getcontext("2d");      } }  function startgame(){     game.start(); } 

the first code snippet not work. canvas null when try context. i'm wondering why? realize why works in second one, well, assume do. it's beeing run in function. canvas attribute in first 1 not being 'run' in second code snippet?

comments , tips appreciated! :d

your first code calls document.getelementbyid("game-screen") in var game declaration.

that's running before element exists.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -