/    Sign up×
Community /Pin to ProfileBookmark

new Array syntax error

Hi, I have this error, and I have no idea where I went wrong.
I’ll post the full html code because I doubt it’s just an error within the javascript.

The error is Line 16 Col 5, which is “var puppies = new Array(6);”

It’s an animation sequence, but for some reason it doesn’t work. Also, when I change onload to onload=”loadAll()” and wrap that function around my entire javascript code, it tells me I need a bracket “)” after the function end bracket “}”

It’s really weird… maybe someone can mess around with it and get back to me?

[QUOTE]

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/URL]“>
<html xmlns=”[URL]http://www.w3.org/1999/xhtml[/URL]“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<link rel=”stylesheet” href=”./styles/dices.css” type=”text/css” />
<title>Run Puppy Run!</title>
</head>
<body onload=”setInterval(“puppyRun()”, 25)”>
<img id=”puppyrun” style=”position: relative;left: inherit;” src=”./images/puppy/puppy0.gif” alt=”A puppy is running” />
<script type=”text/javascript”>
/* <![CDATA[ */
var puppies = new Array(6);
var puppy = document.getElementsByTagName(“img”)[0].src;
var aniCount = 0;

puppies[0] = “./images/puppy/puppy0.gif”;
puppies[1] = “./images/puppy/puppy1.gif”;
puppies[2] = “./images/puppy/puppy2.gif”;
puppies[3] = “./images/puppy/puppy3.gif”;
puppies[4] = “./images/puppy/puppy4.gif”;
puppies[5] = “./images/puppy/puppy5.gif”;
function puppyRun(){
if (aniCount > 5)
aniCount = 0;
else
++aniCount;
puppy = puppies[aniCount].src;
} // puppyRun()
/* ]]> */

</script>

</body>
</html>

[/QUOTE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsFeb 16.2012 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="./styles/dices.css" type="text/css" />
<title>Run Puppy Run!</title>
</head>
<body onload="setInterval(function(){puppyRun(); }, 25)">
<img id="puppyrun" style="position: relative;left: inherit;" src="./images/puppy/puppy0.gif" alt="A puppy is running" />
<script type="text/javascript">
/* <![CDATA[ */
var puppies = [];
puppies[0] = "./images/puppy/puppy0.gif";
puppies[1] = "./images/puppy/puppy1.gif";
puppies[2] = "./images/puppy/puppy2.gif";
puppies[3] = "./images/puppy/puppy3.gif";
puppies[4] = "./images/puppy/puppy4.gif";
puppies[5] = "./images/puppy/puppy5.gif";
var aniCount = 0;


function puppyRun(){
if (aniCount > 5){
aniCount = 0;
}
else {
aniCount++;
}
document.getElementById('puppyrun').src = puppies[aniCount];
} // puppyRun()
/* ]]> */
</script>

</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@xelawhoFeb 16.2012 — untested, but I imagine something like this would work:
[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="./styles/dices.css" type="text/css" />
<title>Run Puppy Run!</title>
</head>
<body onload="setInterval("puppyRun()", 25)">
<img id="puppyrun" style="position: relative;left: inherit;" src="./images/puppy/puppy0.gif" alt="A puppy is running" />
<script type="text/javascript">
/* <![CDATA[ */
var puppies = ["./images/puppy/puppy0.gif","./images/puppy/puppy1.gif","./images/puppy/puppy1.gif","./images/puppy/puppy2.gif","./images/puppy/puppy3.gif","./images/puppy/puppy4.gif","./images/puppy/puppy5.gif"]
var aniCount = 0;

function puppyRun(){
document.getElementById("puppyrun").src = puppies[aniCount++%puppies.length];
} // puppyRun()
/* ]]> */
</script>

</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@nap0leonFeb 16.2012 — The array error is caused by the "6", you don't need it
<i>
</i>var puppies = new Array();


Your code isn't working because when you set a var equal to something.src, the variable becomes a string - the text value of the src of that image, not a reference to that image.

You'll need to fix the paths to your images:
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;link rel="stylesheet" href="./styles/dices.css" type="text/css" /&gt;
&lt;title&gt;Run Puppy Run!&lt;/title&gt;

<i> </i>&lt;script type="text/javascript"&gt;
<i> </i>var aniCount = 0;
<i> </i>var puppies = new Array();
<i> </i>puppies[0] = "00.jpg";
<i> </i>puppies[1] = "01.jpg";
<i> </i>puppies[2] = "02.jpg";
<i> </i>puppies[3] = "03.jpg";
<i> </i>puppies[4] = "04.jpg";
<i> </i>puppies[5] = "05.jpg";

<i> </i>function puppyRun(){
<i> </i> if (aniCount &gt; 5)
<i> </i> aniCount = 0;
<i> </i> else
<i> </i> ++aniCount;

<i> </i> var puppy = document.getElementsByTagName("img")[0];
<i> </i> puppy.src = puppies[aniCount];
<i> </i>}
<i> </i>&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="setInterval('puppyRun()', 1000)"&gt;
&lt;img id="puppyrun" style="position: relative;left: inherit;" src="00.jpg" alt="A puppy is running" /&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@aj_nscFeb 16.2012 — Just throwing it out there, but the following code creates a new instance of an array and, in terms of performance, is faster.

<i>
</i>var my_array = [];

//likewise, to create an object, or hash, whichever you prefer
var my_object = {};
Copy linkTweet thisAlerts:
@foamkittyauthorFeb 20.2012 — Thanks for all the help, it works fine now. I found that the double quotes after the onload event-handler was causing some weird syntax error... maybe it's a depricated command to put the function in double quotes after onload.

Whatever it was, the single quotes removed the syntax error, and it animated perfectly. :3 (with maybe other tweaks to add to it)
×

Success!

Help @foamkitty spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...