By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

naming array values?

P: n/a
Hi, this is an array that is used for a dropdown menu.

var imageArray = new Array(
"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/usa/
1metercalif.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/australia/
parramatta.ecw";

when the menu is displayed it shows the whole value

"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw""

is there a way i can just display a name for the image in the dropdown
menu, like "Massey 2007"

cheers
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Pukeko" <pu************@gmail.comwrote in message
news:ea**********************************@i36g2000 prf.googlegroups.com...
Hi, this is an array that is used for a dropdown menu.

var imageArray = new Array(
"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/usa/
1metercalif.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/australia/
parramatta.ecw";

when the menu is displayed it shows the whole value

"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw""

is there a way i can just display a name for the image in the dropdown
menu, like "Massey 2007"

cheers
How are you building the menu ? If you have code to do that (and it's
always useful to include that in your post), then just adjust to suit.

Otherwise, you haven't provided enough information to answer your queston.

Tim
Jun 27 '08 #2

P: n/a
On May 5, 2:24*am, "Tim Williams" <timjwilliams at gmail dot com>
wrote:
"Pukeko" <pukeko.tani...@gmail.comwrote in message

news:ea**********************************@i36g2000 prf.googlegroups.com...


Hi, this is an array that is used for a dropdown menu.
var imageArray = new Array(
"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/usa/
1metercalif.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/australia/
parramatta.ecw";
when the menu is displayed it shows the whole value
"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw""
is there a way i can just display a name for the image in the dropdown
menu, like "Massey 2007"
cheers

How are you building the menu ? *If you have code to do that (and it's
always useful to include that in your post), then just adjust to suit.

Otherwise, you haven't provided enough information to answer your queston.

Tim- Hide quoted text -

- Show quoted text -
Javascript arrays are a little tricky in this regards. Yes you can
"name" and Array entry, or more accurately assign a "name" as the key.
The problem is, in my experience, that when store array elements in
this manner, the length of the array is not adjusted. For example this
is totally legit:

var images = new Array();
images['Massey 2007'] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_07_fullres.ecw";
images['Massey 2006'] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_06_fullres.ecw";

However if you call alert(images.length) you will get 0. Of course
even if it didn't, this wouldn't help, because you need the key (or
"name") not just the value. And there is no method that I have found
to retrieve the list of keys an array is using. So...the problem isn't
"naming" your array elements, but how do you remember that list of
names so you can later retrieve the values.

You will probably need two arrays, one to store the "names" and the
other to store the "values". How you arrange to coordinate the two are
up to you. You could simply ensure that both arrays are the same
length and that the element 0 in the names array correlates to the
element 0 in the values array, or you could keep the names in array 1
and use the names as the keys in array 2. For example:

var names = new Array();
var urls = new Array();

names.push("Massey 2007");
urls["Massey 2007"] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_07_fullres.ecw";

Then you could loop through the names array and pull the url using
that value as the key from the second array. For example:

for (var i = 0; i < names.length; i++) {
var name = names[i];
var url = urls[name];
//build your Option here...

}

HTH...
Jun 27 '08 #3

P: n/a
Tom Cole <tc****@gmail.comwrote:
>
var names = new Array();
var urls = new Array();
it is easier to use an object :

var myObject={};
myObject[name_1]=url_1;
....
myObject[name_n]=url_n;
that way, unless u've added properties to Object, using
Object.prototype, u can get all the key/value pair by

for(var key in myObject{
alert("key = "+key+", value = "+value);
}
--
Une Bévue
Jun 27 '08 #4

P: n/a
Pukeko wrote:
Hi, this is an array that is used for a dropdown menu.
From your question below I surmise you are probably talking about a `select'
element that only works with present and enabled client-side script support,
which would be a really bad idea.
var imageArray = new Array(
"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw",
document.location has been deprecated more than 10 years ago.
"ecwp://" + document.location.host + "/sampleiws/images/usa/
1metercalif.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/australia/
parramatta.ecw";
Your source code is syntactically incorrect: the closing `)' for the
constructor call is missing. That aside, I would do at least:

var imageArray = new Array(
new Array("ecwp://", "/massey/images/massey/massey_07_fullres.ecw"),
new Array("ecwp://", "/sampleiws/images/usa/1metercalif.ecw"),
new Array("ecwp://", "/sampleiws/images/australia/parramatta.ecw")
);

for (var i = 0, len = imageArray.length; i < len; i++)
{
imageArray[i] = imageArray[i].join(document.location.host);
}
when the menu is displayed it shows the whole value

"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw""

is there a way i can just display a name for the image in the dropdown
menu, like "Massey 2007"
Yes, there is, for example by using your own constructor:

/**
* Constructs a new <code>Item</codeobject.
*
* @param uriParts: Array
* 2-element array containing the scheme and the path of
* the target URI. The parts are joined with the host name
* of the URL of the current document.
* @param text: optional string
* Text for the menu item; the default is the target URI.
* @constructor
*/
function Item(uriParts, text)
{
/**
* URI of the target resource
*/
this.uri = uriParts.join(window.location.host);

/**
* Text for the menu item
*/
this.text = text || this.uri;
}

var imageArray = new Array(
new Item(
new Array("ecwp://", "/massey/images/massey/massey_07_fullres.ecw"),
"Massey 2007"
),
...
);

You should then get informed how HTML `select' and `option' elements work,
or RTFM of your "menu" script of which you have not even posted the relevant
parts here.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jun 27 '08 #5

P: n/a
Tom Cole wrote:
Javascript arrays are a little tricky in this regards. Yes you can
"name" and Array entry, or more accurately assign a "name" as the key.
The problem is, in my experience, that when store array elements in
this manner, the length of the array is not adjusted.
Because you are not storing array elements then, but augment the Array object.
For example this is totally legit:

var images = new Array();
images['Massey 2007'] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_07_fullres.ecw";
images['Massey 2006'] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_06_fullres.ecw";
And why not? Array objects are native objects and, some properties with the
ReadOnly attribute aside, native objects can be augmented with any number of
properties.
However if you call alert(images.length) you will get 0.
It helps to use a collection implementation instead.
Of course even if it didn't, this wouldn't help, because you need the key (or
"name") not just the value. And there is no method that I have found
to retrieve the list of keys an array is using.
var a = ["x", 42];
a["foo"] = "bar";

var out = [];

for (var key in a)
{
var p = a[key];
var t = typeof p;
if (t == "string")
{
p = '"' + p.replace(/"/g, "\\$&") + '"';
}

out.push(key + ": " + t + " = " + p);
}

// displays the enumerable properties of `a'
// and the objects in its prototype chain
window.alert(out.join("\n"));

Note that this can be useful with a collection implementation; if a
collection is not required, then one should use an Object object
instead of an Array object.
So...the problem isn't "naming" your array elements, but how do you
remember that list of names so you can later retrieve the values.
Provided one does not augment Object.prototype or Array.prototype, it is not
as hard to implement as you think.
You will probably need two arrays, one to store the "names" and the
other to store the "values".
No, this is not necessary. You can create user-defined objects that are
elements of the array instead.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.