944,204 Members | Top Members by Rank

Ad:
Nov 29th, 2006
0

Getting funky errors with a simple for loop, anyone care to help?

Expand Post »
Hello All,


I have a javascript image preloading script I'm writing, and it basically creates an array called "array_name" which contains the image filenames, and then uses a for loop to assign "array_name"'s elements to another array called "preload". I know my syntax is good, but the firefox javascript inspector is complaining each time I attempt to run this. Can anyone take a look at the code, and tell me what I'm doing wrong?



Thank You



javascript Syntax (Toggle Plain Text)
  1. function imgLoad() {
  2. array_name=[];
  3. preload=[];
  4. array_name[0]='images/bgcolor.gif';
  5. array_name[1]='images/blackdot.gif';
  6. array_name[2]='images/bold.gif';
  7. array_name[3]='images/centre.gif';
  8. array_name[4]='images/copy.gif';
  9. array_name[5]='images/cut.gif';
  10. array_name[6]='images/email.gif';
  11. array_name[7]='images/hr.gif';
  12. array_name[8]='images/hyperlink.gif';
  13. array_name[9]='images/image.gif';
  14. array_name[10]='images/indent.gif';
  15. array_name[11]='images/insert_table.gif';
  16. array_name[12]='images/italic.gif';
  17. array_name[13]='images/justifyfull.gif';
  18. array_name[14]='images/left_just.gif';
  19. array_name[15]='images/list.gif';
  20. array_name[16]='images/numbered_list.gif';
  21. array_name[17]='images/outdent.gif';
  22. array_name[18]='images/paste.gif';
  23. array_name[19]='images/redo.gif';
  24. array_name[20]='images/right_just.gif';
  25. array_name[21]='images/smiley.gif';
  26. array_name[22]='images/spellcheck.gif';
  27. array_name[23]='images/textcolor.gif';
  28. array_name[24]='images/underline.gif';
  29. array_name[25]='images/undo.gif';
  30. for (var i = 0; i < array_name.length; i++) {
  31. var preload[i] = new Image();
  32. preload[i].src = array_name[i];
  33. }
  34. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
{-_-} is offline Offline
7 posts
since Oct 2006
Nov 29th, 2006
0

Re: Getting funky errors with a simple for loop, anyone care to help?

try replacing:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. array_name=[];
  2. preload=[];
with:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. array_name= new Array();
  2. preload=new Array();

What is the error produced?
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Nov 29th, 2006
0

Re: Getting funky errors with a simple for loop, anyone care to help?

Click to Expand / Collapse  Quote originally posted by MattEvans ...
try replacing:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. array_name=[];
  2. preload=[];
with:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. array_name= new Array();
  2. preload=new Array();
What is the error produced?

The error thats produced is:


missing ; before statement
var preload[i] = new Image();

imgarray.js (line 31)

Though honestly, I think I'm going to ignore this error. I just noticed that the firefox javascript inspector reports no errors with it, however "Firebug" (an extension I have installed) found error with it. If you see something I'm doing wrong, or something that's bad style which would cause Firebug to spit out errors, feel free to let me know, otherwise I'm gonna consider this problem "solved".
Last edited by {-_-}; Nov 29th, 2006 at 4:34 pm. Reason: had to add more
Reputation Points: 10
Solved Threads: 0
Newbie Poster
{-_-} is offline Offline
7 posts
since Oct 2006
Nov 29th, 2006
0

Re: Getting funky errors with a simple for loop, anyone care to help?

ha... the only thing i can think is:
for (var i = 0; i < array_name.length; i++;) {
    var preload[i] = new Image();
    preload[i].src = array_name[i];
}
but hey, i've never seen that last ; put in, and it shouldn't (doesn't need to) be put in really...

EDIT: Actually, this seems wrong/uneccessary:
var preload[i] = new Image();
preload[i] is already indirectly "var'd" when you make the array.
Last edited by MattEvans; Nov 29th, 2006 at 5:41 pm.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Nov 29th, 2006
0

Re: Getting funky errors with a simple for loop, anyone care to help?

Yea, I actually didn't have ; there before, just put it there to see if it would "fix" the problem. I thought that I had to put var in front of all variables that appear within a for loop, so that they stay local? Anyways, thanks for the help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
{-_-} is offline Offline
7 posts
since Oct 2006
Nov 29th, 2006
0

Re: Getting funky errors with a simple for loop, anyone care to help?

thats not a local variable; you are assigning index "i", of the inherited scope/routine scope/public scope array "preload" to reference a new Image object, which is automatically var'd and scoped by that assignment (infact, you'll find that Image isn't accessible by any scope; its scope sort of disapears, and it can only be accessed through the array)

If you were doing this:
for (var i = 0; i < array_name.length; i++) {
    var thisImage = new Image();
    thisImage.src = array_name[i];
    preload[i] = thisImage;
}

then you'd need (or should use) the var.
Last edited by MattEvans; Nov 29th, 2006 at 6:02 pm.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: null is null or not an object error. No menus work
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript syntax '' within '' (simple question)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC