| | |
Getting funky errors with a simple for loop, anyone care to help?
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
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
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)
function imgLoad() { array_name=[]; preload=[]; array_name[0]='images/bgcolor.gif'; array_name[1]='images/blackdot.gif'; array_name[2]='images/bold.gif'; array_name[3]='images/centre.gif'; array_name[4]='images/copy.gif'; array_name[5]='images/cut.gif'; array_name[6]='images/email.gif'; array_name[7]='images/hr.gif'; array_name[8]='images/hyperlink.gif'; array_name[9]='images/image.gif'; array_name[10]='images/indent.gif'; array_name[11]='images/insert_table.gif'; array_name[12]='images/italic.gif'; array_name[13]='images/justifyfull.gif'; array_name[14]='images/left_just.gif'; array_name[15]='images/list.gif'; array_name[16]='images/numbered_list.gif'; array_name[17]='images/outdent.gif'; array_name[18]='images/paste.gif'; array_name[19]='images/redo.gif'; array_name[20]='images/right_just.gif'; array_name[21]='images/smiley.gif'; array_name[22]='images/spellcheck.gif'; array_name[23]='images/textcolor.gif'; array_name[24]='images/underline.gif'; array_name[25]='images/undo.gif'; for (var i = 0; i < array_name.length; i++) { var preload[i] = new Image(); preload[i].src = array_name[i]; } }
try replacing:
with:
What is the error produced?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
array_name=[]; preload=[];
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
array_name= new Array(); preload=new Array();
What is the error produced?
Plato forgot the nullahedron..
•
•
Join Date: Oct 2006
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
try replacing:
with:JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
array_name=[]; preload=[];
What is the error produced?JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
array_name= new Array(); preload=new Array();
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
ha... the only thing i can think is:
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:
preload[i] is already indirectly "var'd" when you make the array.
for (var i = 0; i < array_name.length; i++;) {
var preload[i] = new Image();
preload[i].src = array_name[i];
}EDIT: Actually, this seems wrong/uneccessary:
var preload[i] = new Image(); Last edited by MattEvans; Nov 29th, 2006 at 5:41 pm.
Plato forgot the nullahedron..
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:
then you'd need (or should use) the var.
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.
Plato forgot the nullahedron..
![]() |
Similar Threads
- Winsock Multi-Client Servers (C++)
- nested for loop problem (Java)
- Simple script loop question (Shell Scripting)
- Calculus chat? (Computer Science)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: null is null or not an object error. No menus work
- Next Thread: Javascript syntax '' within '' (simple question)
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser captcha captchaformproblem cart child class close codes column css date debugger decimal dependent design disablefirebug dom download editor element embed engine enter error events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe index java javascript javascripthelp2020 jquery jsf jsp jump libcurl listbox maps masterpage math media menu mp4 object onerror onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post problem programming prototype rated rating redirect safari scale scriptlets scroll search security select software star starrating stars synchronous toggle unicode variables w3c web webservice \n






