admin管理员组

文章数量:1317894

I set the function called "drawEverything()" to run onload, however the function doesn't run and I get a bug in Firebug saying "drawEverything is not defined"?? Thing is, I did define it!

I googled the problem and most of the time it was due to some kind of syntax error, but I've looked all over it and can't find any syntax errors.

My code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<link rel=stylesheet href="style.css" type="text/css">
</head>

<script type="text/javascript">

var new_x = 110;
var new_y = 150;
function drawEverything(){
    var temp_x = new_x;
    var temp_y = new_y;

    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    for (int i = 0; i < 5; i++){
        ctx.beginPath();
        ctx.arc(temp_x,temp_y,20,0,2*Math.PI);

        ctx.stroke();
        if ((i < 3) || (i == 4)){
            temp_x += 40;
        }
        else if (i == 3){
            temp_y += 20;
            temp_x -= 60;
        }
    }
}
</script>

<body onload = "drawEverything()">
<h1>Interactive Olympic Rings</h1>
<div id="container">
<canvas id="myCanvas" width="300" height="300"></canvas>
</div>

</body>
</html>

Any help would be appreciated, thanks!

I set the function called "drawEverything()" to run onload, however the function doesn't run and I get a bug in Firebug saying "drawEverything is not defined"?? Thing is, I did define it!

I googled the problem and most of the time it was due to some kind of syntax error, but I've looked all over it and can't find any syntax errors.

My code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<link rel=stylesheet href="style.css" type="text/css">
</head>

<script type="text/javascript">

var new_x = 110;
var new_y = 150;
function drawEverything(){
    var temp_x = new_x;
    var temp_y = new_y;

    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    for (int i = 0; i < 5; i++){
        ctx.beginPath();
        ctx.arc(temp_x,temp_y,20,0,2*Math.PI);

        ctx.stroke();
        if ((i < 3) || (i == 4)){
            temp_x += 40;
        }
        else if (i == 3){
            temp_y += 20;
            temp_x -= 60;
        }
    }
}
</script>

<body onload = "drawEverything()">
<h1>Interactive Olympic Rings</h1>
<div id="container">
<canvas id="myCanvas" width="300" height="300"></canvas>
</div>

</body>
</html>

Any help would be appreciated, thanks!

Share Improve this question asked Dec 9, 2012 at 11:49 Cloud StrifeCloud Strife 992 gold badges2 silver badges7 bronze badges 1
  • 4 You have your script in the twilight zone, between the head and the body, where nothing can exist! – adeneo Commented Dec 9, 2012 at 11:53
Add a ment  | 

2 Answers 2

Reset to default 5

Deal with your errors in order. Later errors can be a consequence of earlier one.

First error (as reported by firebug):

SyntaxError: missing ; after for-loop initializer
[Break On This Error]   

for (int i = 0; i < 5; i++){

Second error:

ReferenceError: drawEverything is not defined

Since the function definition has pile time errors in it, it is not defined when you e to run it.

You want var not int.

You cannot use int here. Use var in javascript

本文标签: htmljavascript function not definedStack Overflow