I try since 2 hours to change my PHP code into JavaScript for static page "html" but I see example and it's not work. I just want in load page change the backgroung image.
Could you help me?
That my code.
window.onload = choosePic;
var myPix = new Array("images/lion.jpg","images/tiger.jpg","images/bear.jpg");
function choosePic() {
var randomNum = Math.floor(Math.random() * myPix.length);
document.getElementById("myPicture").src = myPix[randomNum];
In my HTML
But nothing works.
It working for me. Here is that code. You can refer to this code and check out whats going wrong The possibility might be that you are giving wrong paths in your Array object, so instead of using those paths, copy paste the paths which I have used in array, check if it is working, if YES then you provided wrong paths in your Array and correct them.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<div>
<img id='myPicture' src=''>
</div>
<script type="text/javascript">
window.onload = choosePic;
var myPix = new Array('https://i.imgur.com/pbyO8uHb.jpg', 'https://i.imgur.com/KgVNwGhb.jpg', 'https://i.imgur.com/Tj1bKhLb.jpg')
function choosePic() {
var randomNum = Math.floor(Math.random() * myPix.length);
document.getElementById("myPicture").src = myPix[randomNum];
}
</script>
</body>
</html>