| Welcome to Webforumz.com. |
|
Jan 14th, 2008, 12:57
|
#1 (permalink)
|
|
Up'n'Coming Member
Join Date: Jan 2006
Location: Belfast
Posts: 57
|
Display different images?
Hi,
Within ASP, is it possible to display different images on a home page?
Ie, whenever you visit the homepage, you either view image 1 or 2 (randomly)?
Thanks in advance,
Ben
|
|
|
Jan 14th, 2008, 20:43
|
#2 (permalink)
|
|
Administrator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 4,102
|
Re: Display different images?
Yes you can use the Rnd function then use an if/else statement to pick an image.
I will try and find an example later.
__________________
Languages: PHP, mySQL (queries), C#, (X)html, CSS, JS.
|
|
|
Jan 14th, 2008, 21:26
|
#3 (permalink)
|
|
Section Manager - WOTM Assistant Editor - LZ
Join Date: May 2007
Location: Cornwall, England
Posts: 1,102
|
Re: Display different images?
|
|
|
Jan 15th, 2008, 02:34
|
#4 (permalink)
|
|
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
|
Re: Display different images?
You could do it with custom made ASP Sub Function or the JavaScript Array function!
JavaScript Array Function:
- Code: Select all
<head>
<script language="JavaScript">
<!-- Hide from old browsers
var imagenumber = 6 ;
var randomnumber = Math.random() ;
var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;
images = new Array
images[1] = "img/pic1.jpg"
images[2] = "img/pic2.jpg"
images[3] = "img/pic3.jpg"
images[4] = "img/pic4.jpg"
images[5] = "img/pic5.jpg"
images[6] = "img/pic6.jpg"
var image = images[rand1]
// -- End Hiding Here -->
</script>
</head>
..
..
<body>
<script language="JavaScript">
// <-- Hide this script from old browsers --
document.write('<IMG SRC="' + image + '">')
// -- End Hiding Here -->
</script>
</body>
ASP Sub Function:
- HTML: Select all
<%
Sub Random_pic()
Dim Pics
Randomize
Pics=Int(Rnd()* 7)
Select Case Pics
Case "0"
Response.write ("img/rdm1.gif")
Case "1"
Response.write ("img/rdm2.gif")
Case "2"
Response.write ("img/rdm3.gif")
Case "3"
Response.write ("img/rdm4.gif")
Case "4"
Response.write ("img/rdm8.gif")
Case "5"
Response.write ("img/rdm6.gif")
Case "6"
Response.write ("img/rdm7.gif")
End Select
End Sub
%>
'Call the sub function using the img tag!
<img src="<%Random_pic()%>">
Or, if you want less code, you can use this trick!
First Step: Give your image a constant name like this:- img/image_1.jpg
- img/image_2.jpg
- img/image_3.jpg
- img/image_4.jpg
- img/image_5.jpg
- img/image_6.jpg
- img/image_7.jpg
- img/image_8.jpg
Second Step: Use the ASP Rnd Function inside your img tag like this:
<img src="img/header_<%=Int(Rnd()* 7)%>.jpg">
This code: <%=Int(Rnd()* 7)%> will generate a random number from 0-7
So the <img src="img/header_RandomNumberGeneratedHere"> will display the image according to the name!
Cool huh?
Cheers...
__________________
|
|
|
Jan 15th, 2008, 07:00
|
#5 (permalink)
|
|
Administrator
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 4,102
|
Re: Display different images?
Not an expert but I think you missed a parenthise on this line:
- Code: Select all
var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;
should read this shouldn't it?
- Code: Select all
var rand1 = Math.round( (imagenumber-1) * randomnumber)) + 1 ;
__________________
Languages: PHP, mySQL (queries), C#, (X)html, CSS, JS.
|
|
|
Jan 15th, 2008, 07:20
|
#6 (permalink)
|
|
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
|
Re: Display different images?
Nope, that code just works fine with me! What the code trying to tell us is...
Math.round((A-1) * B) + 1 ;
First the mathematics function will calculate the (A-1) and later multiply it with B, then plus the multiplication with 1.
Anyway, I got A for my Maths 
__________________
|
|
|
Jan 16th, 2008, 05:15
|
#7 (permalink)
|
|
Most Reputable Member
Join Date: Feb 2004
Location: Borneo
Age: 27
Posts: 1,567
|
Re: Display different images?
Sooo....
Anything new sing2trees? Do you happy with the solution or are you looking for something else?
Cheers...
__________________
|
|
|
| Thread Tools |
|
|
| Rate This Thread |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|