473,657 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP needed, pls read inside

hello group,

I have a long (large) script that shows a random picture when a
webpage is refreshed. It's long because i have a lot of pictures: 246

Here is some code:
-----------------------------------------------------------------------------------------------

<!--
image1="smallpi cs/001-smallpic.jpg"
alt1="Ugly little picture!"

image2="smallpi cs/002-smallpic.jpg"
alt2="Ugly little picture!"

<<<you get the idea???>>>

image244="small pics/244-smallpic.jpg"
alt244="Ugly little picture!"

image245="small pics/245-smallpic.jpg"
alt245="Ugly little picture!"

image246="small pics/246-smallpic.jpg"
alt246="Ugly little picture!"

len=246

now=new Date()
now=now.getSeco nds()
rnd=now%len
image=eval("ima ge"+rnd)
alt=eval("alt"+ rnd)
document.write( "<img src='" + image + "' alt='" + alt + "'
border=0></a>")
//-->

-----------------------------------------------------------------------------------------------

Can someone make it shorter for me?
The number of pics will grow and grow and i don't want to
copy/paste/adjust the [image***=] every time.
The pics are in seperate map on server.
The server does not support php :-(

Hope to hear from you.

tia
kind regards,
Buchw@ld
Feb 19 '06 #1
5 1214
Buchwald said the following on 2/19/2006 12:53 PM:
hello group,

I have a long (large) script that shows a random picture when a
webpage is refreshed. It's long because i have a lot of pictures: 246
Its long because you wrote it wrong. It is also bad code.
Here is some code:
-----------------------------------------------------------------------------------------------

<!--
If all the pictures are named as you have them, the image# variable is
not needed.

image###="small pics/###-smallpic.jpg"

Is the format for the image name. You can generate that without having
to define it 246 times.

Then you create an Array to hold the alt text:

var altText = new Array();
altText['001'] = "Alt text for image 001";
altText['002'] = "Alt text for image 002";

....
altText['245'] = "Alt text for image 245";
altText['246'] = "Alt text for image 246";


len=246
len now equals altText.length, no need to hard-code it.

now=new Date()
now=now.getSeco nds()
rnd=now%len
function Random(x) { return Math.floor(x*Ma th.random()) }
rnd = Random(altText. length);

image=eval("ima ge"+rnd)
eval, and it's use, especially in this case, is indicative of bad code.
It's not needed.

imageRef = window['image' + rnd];

But, it is not needed.
alt=eval("alt"+ rnd)
ditto.
document.write( "<img src='" + image + "' alt='" + alt + "'
border=0></a>")
document.write( '<img src="img'+rnd+' smallpics/'+rnd+'-smallpic.jpg"
border="0" alt="'+altText[rnd]+'">');

all on one line.

-----------------------------------------------------------------------------------------------

Can someone make it shorter for me?


var altText = new Array();
altText['001'] = "Alt text for image 001";
altText['002'] = "Alt text for image 002";
.......
altText['245'] = "Alt text for image 245";
altText['246'] = "Alt text for image 246";

function Random(x) { return Math.floor(x*Ma th.random()) }
rnd = Random(altText. length);
document.write( '<img src="img'+rnd+' smallpics/'+rnd+'-smallpic.jpg"
border="0" alt="'+altText[rnd]+'">');

But, all of that would be better done on the server.
Now, all you have to do is add an altText'###' definition.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 19 '06 #2
JRS: In article <fM************ ********@comcas t.com>, dated Sun, 19 Feb
2006 13:20:43 remote, seen in news:comp.lang. javascript, Randy Webb
<Hi************ @aol.com> posted :

var altText = new Array();
altText['001'] = "Alt text for image 001";
altText['002'] = "Alt text for image 002";
......
altText['245'] = "Alt text for image 245";
altText['246'] = "Alt text for image 246";

function Random(x) { return Math.floor(x*Ma th.random()) }
rnd = Random(altText. length);
document.write ('<img src="img'+rnd+' smallpics/'+rnd+'-smallpic.jpg"
border="0" alt="'+altText[rnd]+'">');

But, all of that would be better done on the server.
Now, all you have to do is add an altText'###' definition.

Was that tested?

There seems to be extra material after src= and the indices should
perhaps be not strings '001' to '246' but numbers 0 to 245 (or add one
to rnd) and rnd may then need extending to three digits.

Question :
The FAQ has that code for generating a random Number in 0..N-1 ; from
that one can build a random string of M digits representing 0..N-1. Is
there a better way, and if so might it be worth having in the FAQ?

S = String(Random(N )+1e10).substr( 11-M)

( String.substr(-M) does not work for me)
OP : please use an informative Subject line.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 19 '06 #3
Dr John Stockton said the following on 2/19/2006 6:17 PM:
JRS: In article <fM************ ********@comcas t.com>, dated Sun, 19 Feb
2006 13:20:43 remote, seen in news:comp.lang. javascript, Randy Webb
<Hi************ @aol.com> posted :
var altText = new Array();
altText['001'] = "Alt text for image 001";
altText['002'] = "Alt text for image 002";
......
altText['245'] = "Alt text for image 245";
altText['246'] = "Alt text for image 246";

function Random(x) { return Math.floor(x*Ma th.random()) }
rnd = Random(altText. length);
document.write( '<img src="img'+rnd+' smallpics/'+rnd+'-smallpic.jpg"
border="0" alt="'+altText[rnd]+'">');

But, all of that would be better done on the server.
Now, all you have to do is add an altText'###' definition.

Was that tested?


Yes it was.

There seems to be extra material after src= and the indices should
perhaps be not strings '001' to '246' but numbers 0 to 245 (or add one
to rnd) and rnd may then need extending to three digits.
Indeed there is extra, unneeded, material after the src=. There are more
errors/problems than just that though.

If the random number is between 1 and 100 then the script will fail. It
needs a routine to pad it to three digits. Dropping the array index as a
string and using digits removes that problem.
Question :
The FAQ has that code for generating a random Number in 0..N-1 ;


The FAQ entry, to me, is misleading.

4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Ma th.random()) }

Implies that Random gives that 1...N range. Even though it says in the
next sentence that it returns 0...(N-1).

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 20 '06 #4
JRS: In article <3-*************** *************** @comcast.com>, dated
Sun, 19 Feb 2006 23:48:14 remote, seen in news:comp.lang. javascript,
Randy Webb <Hi************ @aol.com> posted :
Question :
The FAQ has that code for generating a random Number in 0..N-1 ;


The FAQ entry, to me, is misleading.

4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Ma th.random()) }

Implies that Random gives that 1...N range. Even though it says in the
next sentence that it returns 0...(N-1).

It's not the _next_ sentence; and ISTM that the part which you did not
quote explains, for those who need it, about adding 1. Its full
wording, references apart, is :-
4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Ma th.random()) }

gives a random number in the range 0..(x-1); Random(N)+1 for [1..N]
But you could suggest that " use" should be inserted after the
semicolon.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 20 '06 #5
Dr John Stockton said the following on 2/20/2006 5:42 PM:
JRS: In article <3-*************** *************** @comcast.com>, dated
Sun, 19 Feb 2006 23:48:14 remote, seen in news:comp.lang. javascript,
Randy Webb <Hi************ @aol.com> posted :
Question :
The FAQ has that code for generating a random Number in 0..N-1 ; The FAQ entry, to me, is misleading.

4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Ma th.random()) }

Implies that Random gives that 1...N range. Even though it says in the
next sentence that it returns 0...(N-1).

It's not the _next_ sentence;

My apologies King John. But anybody with common sense wouldn't have been
confused by what I said.
and ISTM that the part which you did not quote explains, for those
who need it, about adding 1. Its full wording, references apart, is :-
4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Ma th.random()) }

gives a random number in the range 0..(x-1); Random(N)+1 for [1..N]
But you could suggest that " use" should be inserted after the
semicolon.


The entry, and its explanation, is misleading. Think what you want.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2747
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like storing products in
0
1123
by: Bruce E. Pullum | last post by:
Ok, I really need some help here.... I have an ASPX page, that contains a save button. It also has an IFRAME that has a tabstrip and multipage control. What I am trying to do is to allow the save button to be visible regardless the page that is loaded in the multipage. This will give the user a single place to save the data that does not change. Here is what I need help with....
9
3136
by: Julia Briggs | last post by:
How do I construct a <iframe> or equivalent for FireFox/NS browsers, inside a screen centered <div> tag? Can it be done?
2
1219
by: Dave | last post by:
I hope someone can help i have a little understanding of Access and need to create a database that will allow me to keep track of employees work hours on various processes involved in building machines. Each employee may be involved in more than one process on more than one machine. I need to input this data for each employee( a total of 10 employees) keeping track of their total hours on each process of the machine. Also i need to add...
13
3699
by: Chua Wen Ching | last post by:
Hi there, I saw this article here in vb.net. http://www.error-bank.com/microsoft.public.dotnet.languages.vb.1/148992_Thread.aspx and http://www.opennetcf.org/forums/post.asp?method=ReplyQuote&REPLY_ID=3922&TOPIC_ID=2224&FORUM_ID=35
0
1891
by: john | last post by:
Hi,All MS Tech Gurus: I need your help!!! It is the second time I post this message, I need get some feedback ASAP, Please Help!! Thanks a lot in advance. John I have a csharp method, using emit to dynamically generate classes & method depends on the meta table in the database,here is my situation 1) In One method I generated the code Dynamic IL Code (Depends on the data pass to it), The generated code works for one dataset but does...
8
1680
by: rh0dium | last post by:
Hi all, I am using python to drive another tool using pexpect. The values which I get back I would like to automatically put into a list if there is more than one return value. They provide me a way to see that the data is in set by parenthesising it. This is all generated as I said using pexpect - Here is how I use it.. child = pexpect.spawn( _buildCadenceExe(), timeout=timeout) child.sendline("somefunction()")
6
1678
by: JOSII | last post by:
Getting a string of boolean value into and out of the registry is no problem. Here's the problem: Although you can place an object into the registry and retreive it, I need to place an ArrayList object with 10 string items into the registry and retreive them later. I tried this: key.SetValue("lstNSXitems", lstNSX.Items) where "lstNSXitems" is the name of the subkey, and lstNSX.Items is the
3
2305
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure I was understanding the chapter on policy classes was to attempt to apply them to my project. I understand the general concept of policies but I lack the knowledge and wisdom of how to identify them in an existing project. So I figured to get an...
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.