Body onload = "" cannot find function

I try to use the philogl library, and when I wrote,

<!DOCTYPE html> <html> <head> <title>PGL2</title> <script type="text/javascript" src="PhiloGL.js"></script> <script type="text/javascript"> function webGLStart(){ alert('I m alive'); } </script> </head> <body onload="webGLStart();"> <canvas id="c" style="width:500px; height:500px;"></canvas> </body> </html> 

everything works fine, but if I write some kind of philologist in it, for example,

 <!DOCTYPE html> <html> <head> <title>PGL2</title> <script type="text/javascript" src="PhiloGL.js"></script> <script type="text/javascript"> function webGLStart(){ var triangle = new PhiloGL.O3D.Model({ vertices: [[0,1,0],[-1,-1,0],[1,-1,0]], colors: [[1,0,0,1],[0,1,0,1],[0,0,1,1]] }); var square = new PhiloGL.O3D.Model({ vertices: [[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0]], colors: [[0.5,0.5,1,1],[0.5,0.5,1,1]],[0.5,0.5,1,1] }); } </script> </head> <body onload="webGLStart();"> <canvas id="c" style="width:500px; height:500px;"></canvas> </body> </html> 

chrome and firefox give me an error that says webGLStart () is not defined. What is wrong with my code?

+4
source share
2 answers

This line:

  colors: [[0.5,0.5,1,1],[0.5,0.5,1,1]],[0.5,0.5,1,1] 

is syntactically incorrect: closing the "]" is in the wrong place. Thus, the definition of the function "failed", so to speak, and the function does not actually exist.

I literally woke up, so I don’t know what is wrong with me, that I could notice it.

+8
source

There must be a mistake in the way you use this "philogl", not allowing you to fully interpret the function. In particular, you have a syntax error with square brackets. Then the function remains undefined.

In the future, you can enable the error console in Firebug (or the equivalent for your preferred browser) to see what you are doing wrong.

Also, prefer document.onload = function() { ... } in the script instead of embedding scripts in HTML tags.

0
source

Source: https://habr.com/ru/post/1341293/


All Articles