js code

T

Treetop

I would like a script to display a list of web links that will be ever
increasing. I don't want to have to do a new document.write command for
each new variable as it comes up. The larger var numbers will be at the
top, so I can add to them as I need to.

var 2l = "hp.com"
var 2t = "HP"

var 1l = " www .ibm.com"
var 1t = "IBM"

..........(and so on)

document.write('<a href=http://');
document.write(1l);
document.write('>');
document.write(1t);
document.write('</a><br>');

repeat some how


I am doing this as I have a palm that can sync js files, so I do updates to
web files when I am away from a computer. I have done this for over a year
now and it works well. I have several cases similar to this that I have to
have to use if/then statements to not display values if they are not there.

I hope that I made sense.
 
H

Hendrik Krauss

I would like a script to display a list of web links that will be ever
increasing. I don't want to have to do a new document.write command for
each new variable as it comes up. The larger var numbers will be at the
top, so I can add to them as I need to.

First recommendation: don't use individual variables, but an array. That is much more convenient and suitable
for your task.

Second recommendation: you can combine individual document.write() statements by using plus signs (+).

May I, thirdly, recommend this code to you:

var links=new Array();
links[0]=new Array("hp.com","Hewlett-Packard");
links[1]=new Array("ibm.com","IBM");
links[2]=new Array("slashdot.org","News for nerds, stuff that matters.");
// and so on ...

for (var i=links.length-1;i>=0;i--)
{
document.write('<a href="http://' + links[0] + '">' + links[1] + '</a><br />');
}

As requested, this will sort the array upside down (i.e. descending by number).
Basically, all you've got to do is adding lines like
links[bla]=new Array("bla","bla");
to the upper part of the script.

(code tested on Win32/Opera 7.20)

Fourth recommendation: Try to avoid subjects like "js code" on comp.lang.javascript ... that's like showing up
at Burger King and loudly proclaiming nothing more than that you'd like to eat something :)
Try to give a clear subject that is specific to your question, or not many people will bother reading it.

Best Regards ;)
Hendrik Krauss
 
T

Treetop

Hendrik Krauss said:
First recommendation: don't use individual variables, but an array. That
is much more convenient and suitable
for your task.

Second recommendation: you can combine individual document.write()
statements by using plus signs (+).
May I, thirdly, recommend this code to you:

var links=new Array();
links[0]=new Array("hp.com","Hewlett-Packard");
links[1]=new Array("ibm.com","IBM");
links[2]=new Array("slashdot.org","News for nerds, stuff that matters.");
// and so on ...

for (var i=links.length-1;i>=0;i--)
{
document.write('<a href="http://' + links[0] + '">' + links[1] +

' said:
}

Fourth recommendation: Try to avoid subjects like "js code" on
comp.lang.javascript ... that's like showing up
at Burger King and loudly proclaiming nothing more than that you'd like to eat something :)
Try to give a clear subject that is specific to your question, or not many people will bother reading it.

Best Regards ;)
Hendrik Krauss


Thank you so much for this, it will help out greatly with my coding. I
wanted to have something in the subject line that talked about this, but did
not have ANY idea what to call it, so thank you for taking the time to help.
For future reference what should I have put in the subject line on this one?

Also, is there a limit on how many characters I can put in an array? One of
the web sites that I manage is a car dealership that has a short description
about the vehicle, make model year of the vehicle, price, and monthly
payments.
 
H

Hendrik Krauss

For future reference what should I have put in the subject line on this one?

How about "Handling great number of variables efficiently?" ... uhm ... I'd say something like
"Scaling this script to deal with many variables" ... maybe "Big amout of data makes coding painful" ... "Bad
efficiency when introducing many variables" ... "Script won't scale well"

Well, something like that. I'm not an authority on subjects, either :)
Take the above proposals, insert own ideas randomly, and throw together your own subject from that. :)
Also, is there a limit on how many characters I can put in an array?

Don't know exactly ... I just conducted a quick test and was able to create two arrays, containing 100000 and
half a million string objects, though that took a while on my old machine:

Creating array w/100,000 elements*
Opera 0.8 sec, IE 6.5 sec

Creating array w/500,000 elements*
Opera 3.3 sec, IE 2 min 40 sec

Didn't check two- or more-dimensional arrays though. (Maybe you've noted that the code I posted contained a
2-d array, which is basically an array containing arrays. Imagine an array as a "list" of things (has one
column), and a 2-d array as a "table" of things (has >1 column)--like the "links" array containing entries
that were in themselves arrays containing name and url.)

So I guess in terms of processing time, element counts with five figures will generally be ok on pretty much
every system; with element counts greater than that you are on the verge of becoming annoying for some users.
Also bear in mind that Javascript is completely client-side, so every chunk of data you may want to access via
script has to be transferred to the client (i.e.: everything must be in your page's source code, which has to
be downloaded prior to execution). So if you shovel, say, the humongous multi-megabyte database (contained in
a Javascript) through the visitor's 56 K connection, you'd better have one heck of a good reason for that :)
You can certainly put a lot of data into the arrays on your page, but keep an eye on the file size.

Good coding!

Best regards
Hendrik Krauss


* all figures: Opera 7.20, Internet Explorer 6.0, Athlon 1.2 GHz, 512 MB RAM, WinXP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,245
Latest member
ShannonEat

Latest Threads

Top