How do i make array that will shuttle and display different a image and content in every reload.- JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do i make array that will shuttle and display different a image and content in every reload.- JavaScript

An array that shows a different image and content in every reload on javascript.

2nd Dec 2021, 1:19 PM
Favour Chidera
Favour Chidera - avatar
7 Answers
+ 1
You mean you want your page to display a random image everytime the page is loaded in browser?
2nd Dec 2021, 1:21 PM
Ipang
+ 1
Are you into doing this on the back-end also, or just on the front-end? I had to ask, cause there will be differences in either way to approach this. If you're only changing an image then use of array will do. But if you're into displaying different content also, well that's where you probably wanna look into back-end stuffs. Cause that sounds like a work of a CMS to me ...
2nd Dec 2021, 1:30 PM
Ipang
+ 1
Favour Chidera Please at least structure your code a bit to make it readable. https://code.sololearn.com/WHHtgsIYTm42/?ref=app
2nd Dec 2021, 6:45 PM
Artur
Artur - avatar
0
Yea ..i kind of blog style whereby: once you reload the page, a different blog image and its content apears
2nd Dec 2021, 1:24 PM
Favour Chidera
Favour Chidera - avatar
0
Hard to tell what's your intension without looking at a code. Try using: window.addEventListener("load", () => { imageSrc = yourImageArray[Math.Floor(Math.Random*yourImageArray.lengh)]; })
2nd Dec 2021, 1:26 PM
cadbrooke
cadbrooke - avatar
2nd Dec 2021, 1:39 PM
Favour Chidera
Favour Chidera - avatar
0
Create an array to store your images, then call the "DOMcontentLoaded" event listener and pass in a function Math.Random() to load a different image upon page reload from your array
4th Dec 2021, 10:41 AM
Alimazoya David
Alimazoya David - avatar