Jump to content

gary769

Member
  • Posts

    1
  • Joined

  • Last visited

Everything posted by gary769

  1. The code you provided looks like it should work for randomly loading a background image on refresh, but it's possible that the issue you're experiencing is related to caching. The browser may be caching the previous image and not updating it with the new one. One way to prevent caching is to add a query parameter to the image URL. This will make the browser think that the image is a new resource and force it to reload the image. Here's an updated version of the code that includes a query parameter: document.addEventListener('DOMContentLoaded', function() { var images = [ 'https://randomwordgenerator.com/img/picture-generator/54e9d1454b5ba914f1dc8460962e33791c3ad6e04e507440752b7edc9649c2_640.jpg', 'https://randomwordgenerator.com/img/picture-generator/50e2dc464e4faa0df7c5d57bc32f3e7b1d3ac3e45658724e77267bd493_640.jpg', 'https://randomwordgenerator.com/img/picture-generator/50e2dc464e4faa0df7c5d57bc32f3e7b1d3ac3e45658724e77267bd493_640.jpg', 'https://randomwordgenerator.com/img/picture-generator/nick-dunlap-h65WkTvyWJY-unsplash.jpg' ].sort(() => .5 - Math.random()); var randImage = images[0]; var imgEl = document.querySelector(".homepage article.sections section:first-child .section-background img"); imgEl.src = randImage + '?v=' + Math.random(); // add a query parameter to prevent caching imgEl.setAttribute("srcset", ""); });
×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.