1

Problem:

The canvas variable in my javascript does not appear to be detecting the canvas tag in my html document and I'm unable to detect the source of the problem.

Information:

I have the following code:

var canvas = document.querySelector('canvas');
console.log(canvas);

And ..

<!doctype HTML>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" type="text/css" href="./style.css">
      <script src="./code.js"></script>
      <title>Canvas Resize</title>
   </head><!-- head -->
   <body>
     <canvas></canvas>
   </body><!-- body -->
</html><!-- html -->

And ..

canvas {

  border: 1px solid black;
}
  • everything was tested with a " console.log("Hello World"); " in the js file and a " * { backgroud-color: blue; } " prior to proceeding with the code that is seen above.

  • Save and refresh have been applied faithfully

I have also done a test in which I replaced the variable's rvalue with a string constant. The value set to the variable was then displayed in the console of two browsers (Chrome and Firefox) - whereas null was rendered with the original rvalue code (ie: with " document.querySelector('canvas'); " as the rvalue).

Request:

Is anyone able to detect what is going wrong here? What am I missing?

Cœur
  • 32,421
  • 21
  • 173
  • 232

1 Answers1

1

Give the canvas a class and query it using document.getElementByClassName( Canvas Class Name)and run it through you console. If that doesn't work, then place the <script> tag at the bottom of the <body> tag to enable the browser load all the DOM objects before running your js code.

Silverman42
  • 111
  • 7