[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

One new Array for several objects?

Posted on 2005-04-21
9
Medium Priority
?
169 Views
Last Modified: 2012-05-05
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
Comment
Question by:zirbel_ch
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 32

Assisted Solution

by:Batalf
Batalf earned 420 total points
ID: 13837793
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 13838260
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13839159
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:zirbel_ch
ID: 13840762
@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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 80 total points
ID: 13840882
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 13841156
And for the first S in KISS was my first empty String element in the Array ;-)
0
 

Author Comment

by:zirbel_ch
ID: 13842875
Thanks to all it was very helpfull.
0
 

Author Comment

by:zirbel_ch
ID: 13843071
@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
 

Author Comment

by:zirbel_ch
ID: 13844389
Ok, I was able to solve the problem.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question