/    Sign up×
Community /Pin to ProfileBookmark

javascript to insert random photo into HTML document

Hi,

Goal of my script: Insert one of four images from an array into an html document without using onLoad()

[CODE]
function get_random() {
//rand function
var ranNum= Math.floor(Math.random()*4);
return ranNum
}
function randomFoto() {

var theFotoIs = get_random()

var muchoFotos = new Array(4)
muchoFotos[0] = “header3.jpg”
muchoFotos[1] = “headerBaker.jpg”
muchoFotos[2] = “header2.jpg”
muchoFotos[3] = “header.jpg”

document.write(muchoFotos[theFotoIs])
}
[/CODE]

The script in the HTML is this:

[CODE]
<script language=”javascript”>

document.write(‘<img src=’ + randomFoto() + ‘>’)
document.write(‘</img>’)
</script>
[/CODE]

The result that I am getting is the file name in the array (i.e. header3.jpg) being printed to the page rather than the html being processed by the browser and I end up with the picture not showing up.

I am guessing there is a very obvious mistake that I have made, I hope so at least. Thank you for help. I do not have the page live so I can not provide a url at this time. Sorry. The code has been tried in FireFox and IE but works in neither.

Thanks again.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@CharlesSep 15.2006 — Is this HTML or XHTML? Because document.write isn't supposed to work in XHTML.
Copy linkTweet thisAlerts:
@TheMusicianauthorSep 15.2006 — Hi Charles,

This is an HTML document. Thanks for letting me know about the XHTML tidbit.
×

Success!

Help @TheMusician 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 5.24,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...