Mackenzie17 Posted October 18, 2012 Share Posted October 18, 2012 (edited) Hi I'm a little new to JavaScript and I am trying to make a small program using an HTML5 canvas. However, when the following code is executed:function Prog(){ var self = this; this.init = function() { var d = document; self.canvas = d.getElementById("can"); self.c = self.canvas.getContext("2d");...}var p = new Prog();p.init();The code works in the other major browsers, but in opera, it throws an error saying "cannot convert self.canvas to object" or sometimes just "self.canvas is null"Thanks in advance Edited October 18, 2012 by Mackenzie17 Link to comment Share on other sites More sharing options...
Ingolme Posted October 18, 2012 Share Posted October 18, 2012 have you checked that d.getElementById("can") is referencing an element? It's possible that you're trying to access the element before it has loaded. Link to comment Share on other sites More sharing options...
Mackenzie17 Posted October 19, 2012 Author Share Posted October 19, 2012 (edited) hi Ingolmethanks for replying, I tried replacingself.c = self.canvas.getContext("2d"); withself.c = d.getElementById("can").getContext("2d"); with that code the error console says"Cannot convert 'd.getElementById("can")' to object"the script is deferred so the page should be loaded before the script is executed thanks Edited October 19, 2012 by Mackenzie17 Link to comment Share on other sites More sharing options...
thescientist Posted October 26, 2012 Share Posted October 26, 2012 (edited) It's possible that you're trying to access the element before it has loaded. how/when are you calling this function in relation to the page loading? Can we see the full code? (JS/HTML) The best practice would be to have it run within the window.onload event handler. Edited October 26, 2012 by thescientist Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now