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

200
Vistas
can I use JS to add a unix timestamp to <img> end of the src?

I need add the random int or unix timestamp to all img src="" whit class='avatar'

<img src="a.png" alt="" class="avatar">
<img src="b.png" alt="" class="avatar">
<img src="c.png" alt="" class="avatar">

I know I can use php to set it <img src="a.png?<?php echo time();?>" alt="" class="avatar">

But I want to try Js to do that.

my code for try, but not work

<script>
var now = Date.now()
var img_src = document.querySelector(".avatar").src;
document.querySelector(".avatar").src = img_src+"?"+now;).
</script>

why add a random int or unix timestamp to the end of img src?

Because I using a CDN

when the user update some photos can't see the new photo on real time.

  1. I prefer use JS don use server side.
  2. the file name is fixed forever, can't change.

any other suggest?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can get all the images with the querySelectorAll method on the document object.

const images = document.querySelectorAll(".avatar");

After that you can change the src attribute for each of them.

images.forEach((image) => {
  image.src = image.src + "?random=" + Date.now();
});
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