I know this question has been asked a few times, but the answers are with onclick events - is it possible to change and image's src tag on page refresh with a predetermined/random source/package of image links?
Asked
Active
Viewed 38 times
-4
-
1Possible duplicate of [Changing the image source using jQuery](https://stackoverflow.com/questions/554273/changing-the-image-source-using-jquery) – Tim Weber Jan 16 '18 at 13:18
-
1Sure it's possible. What have you tried and where are you stuck? – David Jan 16 '18 at 13:23
-
Isn't that an onclick event? - is there an onrestart command alternative? – Ruther Melchor Jan 16 '18 at 13:24
-
Hi David, well - basically on Tim Weber's link, on an onclick event changing rather than a on page refresh alternative. – Ruther Melchor Jan 16 '18 at 13:25
-
1Yes. Have list of image source urls in your code and set one randomly when the script runs. The script will run every time the page loads/refreshes. What have you tried? – Reinstate Monica Cellio Jan 16 '18 at 13:30
-
@RutherMelchor: There is no "on page refresh" event. There also doesn't need to be. Anything that happens when the code loads (such as in a document.ready event) happens on the page refresh. Basically, if you want something to happen when the page loads, just write that code on the page. It'll execute every time the page loads. – David Jan 16 '18 at 13:52
1 Answers
0
I'm not sure as to why StackOverflow turned out to be like this: filled with reputation-greedy people with ego's so high, the site turns to be 'un-helpful'.
Some tabbed this post as duplicate, even if I specifically said that the other one was on click, and I was asking about an on load version and how to execute.
Anyways, here's the solution that I've came with, no frills, no ego, just an answer that will help anyone out there that came to StackOverflow for help:
JAVASCRIPT:
function loadImage(){
var myArray = ["http://link1.gif, http://link2.gif"];
var rand = myArray[Math.floor(Math.random() * myArray.length)];
document.getElementById("imgCon").src= rand.toString();
}
</script>
HTML:
<body onload="loadImage();">
<a href="#"><img id="imgCon"></a>
</body>

Ruther Melchor
- 25
- 4