Admittedly I'm really new to javascript and I assume what I am trying to do is simple but I am having a lot of trouble figuring out how to make this function work with the click of a button instead of a page load/reload.
<script type="text/javascript">
<!--
var ic = 9; // Number of alternative images
var xoxo = new Array(ic); // Array to hold filenames
xoxo[0] = "images/StaminUp.png"
xoxo[1] = "images/DoubleTap.png"
xoxo[2] = "images/QucikRevive.png"
xoxo[3] = "images/ElectricCherry.png"
xoxo[4] = "images/Juggernog.png"
xoxo[5] = "images/SpeedCola.png"
xoxo[6] = "images/MuleKick.png"
xoxo[7] = "images/WidowsWine.png"
xoxo[8] = "images/DeadShot.png"
function pickRandom() {
if (Math.random)
return [Math.floor(Math.random() * xoxo.length)];
else {
var now = new Date();
return (now.getTime() / 1000) % xoxo.length;
}
}
// Write out an IMG tag, using a randomly-chosen image name.
var choice = pickRandom(ic);
// -->
</script>
<html>
<head>
</head>
<body>
<p id="perk"></p>
<script>document.getElementById("perk").innerHTML = ('<img src= "'+xoxo[choice]+'">')</script>
</body>
</html>
from Recent Questions - Stack Overflow http://ift.tt/29Q2nzK
via https://ifttt.com/ IFTTT
No comments:
Post a Comment