Forum Moderators: open

Message Too Old, No Replies

Complex for me

javascript help

         

Fess

3:04 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



Hello,

I have around 20 small images with the exact dimensions in pixels.

I would like to randomly display from the 20, 1 image plus corresponding textual description, everytime a surfer views the webpage. I need the image to be linkable also.

I have searched for the script but the closets I could come up with was the one below.

<script>

//Random link plus text description script

//specify total # of random links
var totallinks=4

var urls=new Array(totallinks)
var descriptions=new Array(totallinks)
var titles=new Array(totallinks)

urls[0]="http://www.eye4u.com"
descriptions[0]="Flash site with a kick"
titles[0]="Inside My Mind"

urls[1]="http://www.xeofreestyle.com"
descriptions[1]="Comprehensive flash and design"
titles[1]="techie"

urls[2]="http://www.billybussey.com/highband.html"
descriptions[2]="All Things Holy. A hand in flash/3d/photoshop"
titles[2]="Dynamic Drive"

urls[3]="http://www.frosch-studio.com"
descriptions[3]="Watch the frog"
titles[3]="The frog"

var MachoManRandomSavage=Math.floor(Math.random()*totallinks)
</script>

<script>
var cont=titles[MachoManRandomSavage]+"<br>"+descriptions[MachoManRandomSavage]
cont+="<br><a href='"+urls[MachoManRandomSavage]+"'>"
cont+=urls[MachoManRandomSavage]+"</a><br>"
document.write(cont)
</script>

<p><font face="arial" size="1">This critiques provided by</font><br>
<a href="http://www.teamphotoshop.com"><font face="arial,helvetica" size="1">Team Photoshop Members</font></a></p>

</body>

The above script, diplays random (4) links plus corresponding textual description.

It been a long time since I've done simple javascript, my arrarys/variables repertoire is minimal.

Can help me to configure this to my needs.
Randomly display 1 linkable image plus corresponding textual description?

Any help is greatly appreciated
Fess

Alternative Future

3:30 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello and Welcome to WebmasterWorld Fess,

All that you require to do is add another Array of image then add this to the outpur string... as below:

var totallinks=4

var image=new Array(totallinks)

image[0]="/img/test1.jpg"

image[1]="/img/test2.jsp"

image[2]="/img/test3.jpg"

image[3]="/img/test4.jpg"

</script>

<script>
var cont=titles[MachoManRandomSavage]+"<br>"+descriptions[MachoManRandomSavage]
cont+="<br><a href='"+urls[MachoManRandomSavage]+"'>"
cont+="<img src='"+image[MachoManRandomSavage]+"'><br>"
cont+=urls[MachoManRandomSavage]+"</a><br>"
document.write(cont)
</script>

Oh and remove the title from its Array and also the output string!

HTH,

-George

Fess

6:07 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



Alternative....

Thanks very much for your help in this matter.

Below, I did what you suggested. It works.

<script>

//Random link plus text description script

//specify total # of random links
var totallinks=4

var image=new Array(totallinks)
var urls=new Array(totallinks)
var descriptions=new Array(totallinks)
var titles=new Array(totallinks)

image[0]="http://www.example.com/image1.jpg"
urls[0]="http://www.eye4u.com"
descriptions[0]="Flash site with a kick"
titles[0]="Inside My Mind"

image[1]="http://www.example.com/image2.jpg"
urls[1]="http://www.xeofreestyle.com"
descriptions[1]="Comprehensive flash and design"
titles[1]="techie"

image[2]="http://www.example.com/image3.jpg"
urls[2]="http://www.billybussey.com/highband.html"
descriptions[2]="All Things Holy. A hand in flash/3d/photoshop"
titles[2]="Dynamic Drive"

image[3]="http://www.example.com/image4.jpg"
urls[3]="http://www.frosch-studio.com"
descriptions[3]="Watch the frog"
titles[3]="The frog"

var MachoManRandomSavage=Math.floor(Math.random()*totallinks)
</script>

<script>
var cont=titles[MachoManRandomSavage]+"<br>"+descriptions[MachoManRandomSavage]
cont+="<br><a href='"+urls[MachoManRandomSavage]+"'>"
cont+="<img src='"+image[MachoManRandomSavage]+"'><br>"
cont+=urls[MachoManRandomSavage]+"</a><br>"
document.write(cont)
</script>

The only thing I'm confused about is the following:
"Oh and remove the title from its Array and also the output string!"

You suggested I remove the title? The title meaning "totallinks"?

Also, I would like to have the URLS to say "click here" and link them to the approtiate pages.

I tried to insert this into to the output string but it did not work:

urls[0]="<a href="http://www.something.com" target="_blank">Click Here</a>"

any suggestions?

Thanks
Fess

steelegbr

8:52 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



urls[0]="<a href="http://www.something.com" target="_blank">Click Here</a>"

You cannot emebed quotation marks as above. You should use:

urls[0]="<a href='http://www.something.com' target='_blank'>Click Here<\/a>"

(Single quotes around _blank and URL)

Fess

10:08 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



Steelgbr...

I understand. Thanks for the correction.

Although when I place it into the output string:

urls[0]="<a href='http://www.something.com' target='_blank'>Click Here</a>"

The resulting html page, looks like this:

Click Here'>
Click Here

I suspect the tags are not suited for the array/variable or something.

Do you have any suggestions?

Thank You
Fess

Alternative Future

10:14 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ignore my first remark on removing the title if it works :)

As for your latest problem I think you may have to escape the forward slash as suggested by steelegbr
i.e.
<\/a>
It allows you to write the forward slash without it being mistaken for a division sign!

-George

Fess

10:44 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



George

I corrected with <\/a>
Proceeded to place it into the output string:

urls[0]="<a href='http://www.something.com' target='_blank'>Click Here</a>"

The resulting html page still looks like this:

Click Here'>
Click Here

What am I don't wrong?

Thanks
Fess

Alternative Future

10:49 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Can you not do it the original way of:-
urls[0]=http://www.something.com

With the output handling the target="_blank" and click here
i.e.

cont+="<br><a href='"+urls[MachoManRandomSavage]+"' target='_blank'>Click here</a><br>"

If you post your current <script> where the document write is I shall see if I can ammend it to do what you are after.

-George

Fess

9:13 pm on Mar 17, 2004 (gmt 0)

10+ Year Member



George...

Your suggestion worked out great. The script is functional.

Its great that I can get this help for other folks in this forum.

Thank you again for your help in this matter.

Regards
Fess

Alternative Future

9:16 pm on Mar 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Fess,

No problem, I get a lot of help too, from everyone on here.

Only too glad I can pay it back...

-George