/    Sign up×
Community /Pin to ProfileBookmark

image rotation on entry

Mr. J
I’ve not been successful in getting the script to work. here is my code:

<script language=”javascript”>
<!–
mypics=new Array
(“1.jpg”, “2.jpg”, “3.jpg”)
function show_pic(){
randomNum=Math.floor(Math.random() * mypics.length);
document.getElementById(“myimage”).src=mypics[randomNum];
alert(randomNum)
}
//–>
</script>

<img id=”myimage” src=”1.jpg”>

all put inside the body tags. The only picture that shows is the first one, even on refresh or re-entry.

Can you help?

Thanks, Janna

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonJul 15.2003 — Change this line:

mypics=new Array

to this:

var mypics=new Array();


And also you may want to change this line:

randomNum=Math.floor(Math.random() * mypics.length);

to this:

randomNum=Math.round(Math.random() * (mypics.length-1));
Copy linkTweet thisAlerts:
@JannaauthorJul 15.2003 — this is what my code looks like now:

<script language="javascript">
<!--

var mypics=new Array();

("1.jpg", "2.jpg", "3.jpg");

function show_pic(){

randomNum=Math.round(Math.random() * (mypics.length-1));

document.getElementById("myimage").src=mypics[randomNum];

alert(randomNum)

}

//-->

</script>

<img id="myimage" src="1.jpg">



Still the image does not rotate on entry, it always only has the first picture up. All the pics are in the same place and I've checked their names, ha, 1,2,3...

Incase this is the problem, here is a copy of my body tag

<body bgcolor="#ffffff"ONLOAD="preloadImages();" onload="show_pic()">

I am also using JavaScript1.2, does that matter?

Thanks for the help, Janna?
Copy linkTweet thisAlerts:
@CharlesJul 15.2003 — [font=georgia]You cannot change the "src" property of an image until it exists but you can draw it with a random "src" attribute in the first place. Put his in the document's HEAD element:

[font=monospace]<script type="text/javascript">

<!--

Array.prototype.random = function () {return this[Math.round (Math.random() * Math.ceil (this.length / 10) * 10) % this.length]}

// -->

</script>[/font]

And this where you want the image to appear.

[font=monospace]<script type="text/javascript">

<!--

document.write('<img src="', ['1.jpg', '2.jpg', '3.jpg'].random(), '" alt="[a random image]">');

// -->

</script>

<noscript><img src="noScript.jpg" alt="[default image]"></noscript>[/font][/font]
Copy linkTweet thisAlerts:
@JannaauthorJul 15.2003 — Thank you, that worked!!! ?

Thanks again. Janna
Copy linkTweet thisAlerts:
@JannaauthorJul 28.2003 — Charles:

I'm not sure what happened, but I can not get this to work again. Can you tell me what is most likely my problem. ha, you know what I mean.

<head>

<script type="text/javascript">

<!--

Array.random = function () {return this[Math.round

(Math.random() * Math.ceil (this.length /10) * 10 %

this length]}

// --></script>

</head>

<body

<script type="text/javascript">

<!--

document.write ('<img src="' , ['1.jpg', '2.jpg', '3.jpg'].random(),

'" alt="[random Fish & Wildlife Image]">');

// --> </script>

<noscript><image src="1.jpg" alt="[default image]"></noscript>


I am putting the images on the same level as the page that I'm inserting the code into. BUT would like to put them in a folder, can you also tell me where I'd write that?

Thanks, Janna
Copy linkTweet thisAlerts:
@CharlesJul 28.2003 — [font=georgia]From here it looks like you've dropped the '>' in your BODY tag.[/font]
Copy linkTweet thisAlerts:
@JannaauthorJul 28.2003 — Mr. J

I got the script to work, thank you. Now, I'd like to put the images in folders. When I do this, I loose them. Will you tell me where to insert my folder name? Thanks, Janna
Copy linkTweet thisAlerts:
@JannaauthorJul 28.2003 — Hi Charles:

On my page, I do have the body tag inclosed. Something else is wrong, but I can not find it. Thanks, Janna

Any other help will be appreciated.

j
Copy linkTweet thisAlerts:
@CharlesJul 28.2003 — [font=georgia]Since your posting does not reflect what is on your page, you would do well to give me the URL of the page.[/font]
Copy linkTweet thisAlerts:
@JannaauthorJul 29.2003 — Sorry Charles, I really appreciate your help. I am working on the Intranet, right now I don't have anywhere to post. We are due to shut down, I'm Federal, today at 9:00am, so I don't really have enough time to do anything else. (Like get space somewhere.) If we don't shut down and I can, I'll post and let you know. Thanks, Janna
×

Success!

Help @Janna spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...