Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda