Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Jascript random background static page HTML

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!