Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-05-2012, 06:57 PM   PM User | #1
Vrutin
New Coder

 
Join Date: Dec 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Vrutin is an unknown quantity at this point
Exclamation How to change to new image everytime..

Hey, I have just started scripting JavaScript and I got stuck when I wanted to change everytime the page is reloaded to have a new image.

Here is my current script:

Code:
var index = 1;

function randomContent() {
	var mainContent = document.getElementById("mainContent");
	var randomImg="images/content" + index++ + ".png";
	mainContent.src=randomImg;
}

window.onload = function() {
	// Load Random Images
	randomContent();
}
It displays the first image. However, it doesn't change every-time to a new one.
Vrutin is offline   Reply With Quote
Old 12-05-2012, 08:31 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
the index will always be the same unless you save information somehow between page views. you could use a cookie ( http://www.tutorialspoint.com/javasc...pt_cookies.htm ) to record and use the information perhaps. This will work on a per user basis.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 12-05-2012, 08:45 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,234
Thanks: 59
Thanked 3,997 Times in 3,966 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You could also simply choose a random index value. It is then *possible* that a user will get the same image 2 times in a row, but the odds of that will only be 1 in N, where N is how many available images you have.

It's not perfect, but it's simpler and may work well enough.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-05-2012, 08:57 PM   PM User | #4
Vrutin
New Coder

 
Join Date: Dec 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Vrutin is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
You could also simply choose a random index value. It is then *possible* that a user will get the same image 2 times in a row, but the odds of that will only be 1 in N, where N is how many available images you have.

It's not perfect, but it's simpler and may work well enough.
Hey, I later on decided to do that ... its working as how I wanted, though I am getting some 0 when number is generated..

Code:
var index = Math.round(Math.random()*3);
if(index == 0)
{
    index = Math.round(Math.random()*3);
}

function randomContent() {
	var mainContent = document.getElementById("mainContent");
	var randomImg="images/content" + index + ".png";
	mainContent.src=randomImg;
}

window.onload = function() {
	// Load Random Images
	randomContent();
}
Go it working by changing to:
Code:
function randomContent() {
	var index = Math.ceil(Math.random() * 3);
	var mainContent = document.getElementById("mainContent");
	var randomImg = "images/content" + index + ".png";
	mainContent.src = randomImg;
}

Last edited by Vrutin; 12-05-2012 at 09:08 PM..
Vrutin is offline   Reply With Quote
Old 12-05-2012, 09:05 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,234
Thanks: 59
Thanked 3,997 Times in 3,966 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
VERY WRONG:
Code:
var index = Math.round(Math.random()*3);
if(index == 0)
{
    index = Math.round(Math.random()*3);
}
RIGHT:
Code:
var numberOfImages = 3; // or however many you have
var index = 1 + Math.floor( Math.random() * numberOfImages );
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-07-2012, 02:39 PM   PM User | #6
Vrutin
New Coder

 
Join Date: Dec 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Vrutin is an unknown quantity at this point
Thanks For the advice..
Vrutin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:33 AM.


Advertisement
Log in to turn off these ads.