body onload="" cannot find function
i am trying stuff with 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>开发者_如何学Go;
</body>
</html>
everything works fine, but if i write some philogl in it like,
<!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 gives me an error that says webGLStart() is not defined. What is wrong with my code?
This line:
colors: [[0.5,0.5,1,1],[0.5,0.5,1,1]],[0.5,0.5,1,1]
is syntactically incorrect: a closing "]" is in the wrong place. Thus the function definition "failed", so to speak, and the function doesn't really exist.
I literally just woke up so I'm not sure what's wrong with me that I could spot that.
There must be an error in the way you are using this "philogl", preventing the function from being interpreted fully. In particular, you have a syntax error with your square brackets. The function then remains undefined.
In future you can turn on the error console in Firebug (or the equivalent for your preferred browser) to see what you're doing wrong.
Also, prefer document.onload = function() { ... }
in a script, instead of embedding scripting in HTML tags.
精彩评论