Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello can some guru be so kind to convert this to Javascript please 6

Status
Not open for further replies.
Jul 13, 2001
180
US
<script language=vbs>
dim myPics
sub setPics()
myPics = array (&quot;Pic1.jpg&quot;,&quot;Pic2.jpg&quot;,&quot;Pic3.jpg&quot;,&quot;Pic4.jpg&quot;,&quot;Pic5.jpg&quot;)
outStr = &quot;&quot;
randomize()
do while uBound(myPics) > 0
ranNum = fix(Rnd() * (uBound(myPics)+1))
outStr = outStr & &quot;<img src='&quot; & myPics(ranNum) & &quot;' title='&quot; & myPics(ranNum) & &quot;'>&quot;
resetArray(ranNum)
loop
'add remaining picture
outStr = outStr & &quot;<img src='&quot; & myPics(0) & &quot;' title='&quot; & myPics(0) & &quot;'>&quot;
document.getElementById(&quot;picPlace&quot;).innerHTML = outStr
end sub

sub resetArray(usedVal)
dim tempArr(), arrCount
arrCount = 0
for x = 0 to uBound(myPics)
if x <> usedVal then
redim preserve tempArr(arrCount)
tempArr(arrCount) = myPics(x)
arrCount = arrCount + 1
end if
next
redim myPics(uBound(tempArr))
for x = 0 to uBound(tempArr)
myPics(x) = tempArr(x)
next
end sub
</script>
<body onLoad=&quot;setPics()&quot;>
<div id=&quot;picPlace&quot;></div>


Thanks in advance!
 
try this:
Code:
<script type=&quot;text/javascript&quot;>
function setPics() {
	var myPics = [&quot;Pic1.jpg&quot;,&quot;Pic2.jpg&quot;,&quot;Pic3.jpg&quot;,&quot;Pic4.jpg&quot;,&quot;Pic5.jpg&quot;];
	var arOut = [];

	while (myPics.length > 0) {
		var ranNum = Math.round(Math.random() * (myPics.length - 1));

		arOut.push(&quot;<img src='&quot; + myPics[ranNum] +
			&quot;' title='&quot; + myPics[ranNum] +
			&quot;' alt='&quot; + myPics[ranNum] + &quot;'/>&quot;);

		myPics.splice(ranNum, 1);
	}
	document.getElementById(&quot;picPlace&quot;).innerHTML = arOut.join(&quot;&quot;);
}
</script>
<body onLoad=&quot;setPics();&quot;>
<div id=&quot;picPlace&quot;></div>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
jeff,

Thanks for the array workshop, if you don't mind
could you explain why you generated the arrays
using
Code:
[,,,]
instead of (,,,) is there a difference.

Good Code!

2b||!2b
 
very nice jemminger!



_____________________________________________________________________
onpnt2.gif

 
Okay, here's a toughie.
I have breaking my head trying to make it them rollovers and not just static images. Any chanc of that??

Thanks again.

So much amazing talent here.
 
Nice one jemminger :) Pretty funky combining the randomising with the outputting of the <img tags

Randomly sorting arrays is amazingly easy, but my <img output isn't as elegant:

Code:
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function setPics() {
	var myPics = [&quot;Pic1.jpg&quot;,&quot;Pic2.jpg&quot;,&quot;Pic3.jpg&quot;,&quot;Pic4.jpg&quot;,&quot;Pic5.jpg&quot;];
	myPics = myPics.sort(randomSort);

	var imageList =  (&quot;|&quot; + myPics.join(&quot;|&quot;)).replace(/\|(\w+\.\w+)/g, &quot;<img src='$1'>&quot;);
	document.getElementById(&quot;picPlace&quot;).innerHTML = imageList;
}
function randomSort() {
	return Math.floor(Math.random()*3)-1; //randomly -1, 0 or 1
}
</script>
</head>
<body onLoad=&quot;setPics();&quot;>
<div id=&quot;picPlace&quot;></div>

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
nice sort function clarkin - i haven't gotten the hang of those yet

good use of regex too!


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
<script type=&quot;text/javascript&quot;>
function setPics() {
var myPics = [&quot;Pic1.jpg&quot;,&quot;Pic2.jpg&quot;,&quot;Pic3.jpg&quot;,&quot;Pic4.jpg&quot;,&quot;Pic5.jpg&quot;];

var arOut = [];

while (myPics.length > 0) {
var ranNum = Math.round(Math.random() * (myPics.length - 1));

arOut.push(&quot;<img src='&quot; + myPics[ranNum] +
&quot;' title='&quot; + myPics[ranNum] +
&quot;' alt='&quot; + myPics[ranNum] + &quot;'&quot; +
&quot; onMouseOver = 'newPic(this,true)' &quot; +
&quot; onMouseOut = 'newPic(this,false)' />&quot;);


myPics.splice(ranNum, 1);
}
document.getElementById(&quot;picPlace&quot;).innerHTML = arOut.join(&quot;&quot;);
}


function newPic(inObj, inBool){
//function assumes rollover image has same name as
//original image plus &quot;XX&quot; at the end pic1.jpg --> pic1XX.jpg

picName = inObj.src.substr(0,inObj.src.indexOf(&quot;.&quot;)-1)
picExt = inObj.src.substring(inObj.src.indexOf(&quot;.&quot;) ,inObj.src.length)

if (inBool){
inObj.src = picName + &quot;XX&quot; + picExt
}
else{
inObj.src = picName.substr(0,picName.length - 2) + picExt
}
}

</script>
<body onLoad=&quot;setPics();&quot;>
<div id=&quot;picPlace&quot;></div>


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Awesome!!
At first I could not get it to work, just missed a bracket or two and changed some of the number in the strings subtraction:
YOU ARE BRILLIANT:

Someone please make this a TUT to post here please. :)

Thank you so much!!!


<script type=&quot;text/javascript&quot;>
function setPics() {
var myPics = [&quot;Pic1.jpg&quot;,&quot;Pic2.jpg&quot;,&quot;Pic3.jpg&quot;,&quot;Pic4.jpg&quot;,&quot;Pic5.jpg&quot;];

var arOut = [];

while (myPics.length > 0) {
var ranNum = Math.round(Math.random() * (myPics.length - 1));

arOut.push(&quot;<img src='&quot; + myPics[ranNum] +
&quot;' title='&quot; + myPics[ranNum] +
&quot;' alt='&quot; + myPics[ranNum] + &quot;'&quot; +
&quot; onMouseOver = 'newPic(this,true)' &quot; +
&quot; onMouseOut = 'newPic(this,false)' />&quot;);

myPics.splice(ranNum, 1);
}
document.getElementById(&quot;picPlace&quot;).innerHTML = arOut.join(&quot;&quot;);
}


function newPic(inObj, inBool){
//function assumes rollover image has same name as
//original image plus &quot;XX&quot; at the end pic1.jpg --> pic1XX.jpg

picName = inObj.src.substr(0,inObj.src.indexOf(&quot;.&quot;))
picExt = inObj.src.substring(inObj.src.indexOf(&quot;.&quot;) ,inObj.src.length)

if (inBool){
inObj.src = picName + &quot;XX&quot; + picExt
}
else{
inObj.src = picName.substr(0,picName.length - 2) + picExt
}
}

</script>
 
Oops, her eis the correct version:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>

<script type=&quot;text/javascript&quot;>
function setPics() {
var myPics = [&quot;Pic1.jpg&quot;,&quot;Pic2.jpg&quot;,&quot;Pic3.jpg&quot;,&quot;Pic4.jpg&quot;,&quot;Pic5.jpg&quot;];

var arOut = [];

while (myPics.length > 0) {
var ranNum = Math.round(Math.random() * (myPics.length - 1));

arOut.push(&quot;<img src='&quot; + myPics[ranNum] +
&quot;' title='&quot; + myPics[ranNum] +
&quot;' alt='&quot; + myPics[ranNum] + &quot;'&quot; +
&quot; onMouseOver = 'newPic(this,true)' &quot; +
&quot; onMouseOut = 'newPic(this,false)' />&quot;);

myPics.splice(ranNum, 1);
}
document.getElementById(&quot;picPlace&quot;).innerHTML = arOut.join(&quot;&quot;);
}


function newPic(inObj, inBool){
//function assumes rollover image has same name as
//original image plus &quot;XX&quot; at the end pic1.jpg --> pic1XX.jpg

picName = inObj.src.substr(0,inObj.src.indexOf(&quot;.&quot;))
picExt = inObj.src.substring(inObj.src.indexOf(&quot;.&quot;) ,inObj.src.length)

if (inBool){
inObj.src = picName + &quot;XX&quot; + picExt
}
else{
inObj.src = picName.substr(0,picName.length - 2) + picExt
}
}

</script>
</head>
<body onLoad=&quot;setPics();&quot;>
<div id=&quot;picPlace&quot;></div>

</body>
</html>
 
OMG, I forgot one thing. how do you add hyperlinks to the appropriate image? An array, with 2 elements?
 
Okay I diddled around with my limited knowledge of Javascriot and came up with this . It works! BUT something is amiss.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>

<script type=&quot;text/javascript&quot;>


function setPics() {
// var newpics = new Array(5);
var myPics = new Array(5)
myPics[0] = &quot;Pic1.jpg&quot;
myPics[1] = &quot;Pic2.jpg&quot;
myPics[2] = &quot;Pic3.jpg&quot;
myPics[3] = &quot;Pic4.jpg&quot;
myPics[4] = &quot;Pic5.jpg&quot;
var url = new Array(5)
url[0] = &quot;url[1] = &quot;url[2] = &quot;url[3] = &quot;url[4] = &quot;
var arOut = [];

while (myPics.length > 0) {
var ranNum = Math.round(Math.random() * (myPics.length - 1));

arOut.push(&quot;<a href=&quot; + url[ranNum]+&quot;><img src='&quot; + myPics[ranNum] +
&quot;' title='&quot; + myPics[ranNum] +
&quot;' alt='&quot; + myPics[ranNum] + &quot;'&quot; +
&quot; onMouseOver = 'newPic(this,true)' &quot; +
&quot; onMouseOut = 'newPic(this,false)' /></ahref><br>&quot;);

myPics.splice(ranNum, 1);
}
document.getElementById(&quot;picPlace&quot;).innerHTML = arOut.join(&quot;&quot;);
}


function newPic(inObj, inBool){
//function assumes rollover image has same name as
//original image plus &quot;XX&quot; at the end pic1.jpg --> pic1XX.jpg

picName = inObj.src.substr(0,inObj.src.indexOf(&quot;.&quot;))
picExt = inObj.src.substring(inObj.src.indexOf(&quot;.&quot;) ,inObj.src.length)

if (inBool){
inObj.src = picName + &quot;XX&quot; + picExt
}
else{
inObj.src = picName.substr(0,picName.length - 2) + picExt
}
}

</script>
</head>
<body onLoad=&quot;setPics();&quot;>
<div id=&quot;picPlace&quot;></div>

</body>
</html>
 
</ahref> should be </a>

Also you might find the shorthand way of creating an array easier:
var myPics = new Array(&quot;Pic1.jpg&quot;, &quot;Pic2.jpg&quot;, ...)

When you add to it you just throw in some elements at the end :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
You might want to add a single ' around link url.

2b||!2b
 
Sorry that was confusing.

You need to add a pair of ' one on each end of
the link url.

you want the link to look like:

(&quot;<a href='&quot; + url[ranNum]+ &quot;'><

2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top