• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

One new Array for several objects?

In a page there are about 10 click-buttons. Each of them opens a small window (of identical size and specifications) which gives short explanation about the item.

A part of the script in the page is written like this:
----------
Act['01'] = new Array(OpenWin,'01.html','',550,700,1,1,0,0,0,0,0);
Act['02'] = new Array(OpenWin,'02.html','',550,700,1,1,0,0,0,0,0);
.
.
Act['10'] = new Array(OpenWin,'10.html','',550,700,1,1,0,0,0,0,0);
----------
Is there a good syntaktikal way to write the same in single expression?

("Act" is a new object, "OpenWin" is a function which builds and opens the new window.)
0
zirbel_ch
Asked:
zirbel_ch
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
BatalfCommented:
Something like this?

var Act = new Array();

for(var no=0;no<10;no++){
    var prefix="";
    if(no<10)prefix="0";
    Act[prefix+no] = new Array(OpenWin,prefix+no+'.html','',550,700,1,1,0,0,0,0,0);
}

I assume that "OpenWin" is a defined variable!?
0
 
ZvonkoSystems architectCommented:
There is a ONE statement version:

<script>

var Act = [ "",
[OpenWin,'01.html','',550,700,1,1,0,0,0,0,0],
[OpenWin,'02.html','',550,700,1,1,0,0,0,0,0],
[OpenWin,'03.html','',550,700,1,1,0,0,0,0,0]];

</script>

0
 
Michel PlungjanIT ExpertCommented:
do NOT use numbers as string index. 08 and 09 are considered invalid octal numbers
you can always remove the leading 0 from whereever you need to access them from
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
zirbel_chAuthor Commented:
@mplungjan

What do you propose instead of the indexes '01', '02' etc. to become a logical suite which goes with Batalf's proposition (which works fine)?
Does-it make-it better with an alphabetical praefix like 'a01', a02' etc.?
0
 
Michel PlungjanIT ExpertCommented:
Yes a LOT better since javascript will attempt to loose the leading 0 as often as possible, the 08 and 09 are as mentioned illegal octal numbers and all numbers with leading 0 are considered octal in javascript which means you need  ,10 on parseInt to allow them:
Num = "08"
alert(parseInt(Num))
alert(parseInt(Num,10))

You can also just use
0,1,2

which is how js likes it and which makes the array simpler to handle than the associative array. KISS principle
0
 
ZvonkoSystems architectCommented:
And for the first S in KISS was my first empty String element in the Array ;-)
0
 
zirbel_chAuthor Commented:
Thanks to all it was very helpfull.
0
 
zirbel_chAuthor Commented:
@Batalf

Sorry, there is still a little problem with the 3 help-windows which should be called with the same function. That is how the script in the page looks (accepting the comment of mplungjan about leading 0):
-----
Act['help_en'] = new Array(OpenWin,'help_en.html','',300,350,1,1,0,0,0,0,0);
Act['help_de'] = new Array(OpenWin,'help_de.html','',300,350,1,1,0,0,0,0,0);
Act['help_fr'] = new Array(OpenWin,'
help_fr.html','',300,350,1,1,0,0,0,0,0);
      var Act = new Array();
      for(var no=0;no<20;no++){
Act[no] = new Array(OpenWin,no+'.html','',550,700,1,1,0,0,0,0,0);
----
Unfortunately the help-pages don't work anymore. But why?
0
 
zirbel_chAuthor Commented:
Ok, I was able to solve the problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now