PDA

View Full Version : I need help with Javascript.


Sir Penguin
03-04-2005, 03:01:59
Here's a snippet of some Javascript I'm writing:
for (i=0; i<NAMESIZ; i++) {
// if names[i] is a string, make it an Option() and add it to the box.
// otherwise, make a blank-string Option() and add it instead.
// -- this Option() probably shouldn't be created each time?
if (instanceOf(names[i],String)) {
members.options[i+2] = new Option(indent+names[i]);
} else {
members.options[i+2] = new Option("");
}
}
The instanceOf function is previously defined, and works like the Java method. The members variable is a selectbox, and the names array contains strings. The NAMESIZ variable is the maximum size of the names array, and the indent variable is a string. I have inserted two lines into the members.options array.

But all that doesn't really matter. What I need to know is what to make the indent string so that it will indent the lines by four spaces. I've tried " " and "&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;" but neither of them work. The former doesn't insert any spaces, and the latter inserts the literal character codes instead of the character.

Any ideas?

SP

Qaj the Fuzzy Love Worm
03-04-2005, 08:15:43
How about %20?

Sir Penguin
03-04-2005, 09:52:44
Nope.

SP

Nav
03-04-2005, 13:39:46
use %26 instead of & ?

Or just do as this forum does and use a couple of hyphens to simulate an indent.

-- Option

Qaj the Fuzzy Love Worm
03-04-2005, 17:55:07
*nod* I think that's what I had to do as a kludge at work. Doesn't look too bad either.

Or, and this is really sucky but works, use character 255. On IE at least it displays a space, but I have no idea what it would do on anything else. This is a good time to DL the latest Firefox, I think :)

Qaj the Fuzzy Love Worm
03-04-2005, 18:06:29
Works in firefox, too.

Sir Penguin
03-04-2005, 23:54:31
I'm using the hyphens for something else. It's supposed to look like this:

-- Owner
OwnerName
-- Members
Member 1
Member 2
Member 3
...
-- Pending
Pending 1
...


I guess I could change the specs though, that's a good idea. I'll try those other things first.

edit: well, it didn't work. I guess I'll move the hyphens down. Thanks for the help.

SP

Sir Penguin
04-04-2005, 07:30:09
If I have the following code in the body section of my HTML file (as produced by PHP), will it be executed when the page loads?

<script type='text/javascript' language='JavaScript'><!--
var NAMESIZ = 15;
g1names = new Array(NAMESIZ);
g2names = new Array(NAMESIZ);
g3names = new Array(NAMESIZ);
g1names[g1names.length] = 'Adam Smith';
g1names[g1names.length] = 'Benny Chan';
g1names[g1names.length] = 'Candy Dulfer';
g1names[g1names.length] = 'David Tao';
g1names[g1names.length] = 'Dennis LAU';
g1names[g1names.length] = 'Eddie Kwan';
g1names[g1names.length] = 'Fanny Chow';
g1names[g1names.length] = 'Kiff Man';
g2names[g2names.length] = 'Gary Chang';
g2names[g2names.length] = 'Henry Duke';
g2names[g2names.length] = 'Ivy Low';
g3names[g3names.length] = 'Jason Wu';
...

SP

Funko
04-04-2005, 09:04:34
It will be executed when the script is written to the page - ie. during the page load but before the entire page is loaded.

As this is just setting up variables that should be fine. If you were running a script that referenced items on the page it'd have to run after those items appeared.

Not great practice to have that in the body but I've had to do that sometimes with automated generators.