JS-code that make video backgrounds to work on all devices

Hi.
I would like to share a little JS-code that make video backgrounds to work on all devices.
Feel free to use it.

I use this JS-code on this website: www.stayalive.se

const $videos = [...document.querySelectorAll(".x-video.player script")];

$videos.map( video => {
    video.innerText = video.innerText.replace(" muted>"," muted playsinline>");
});

window.addEventListener('load', () => {
  const videos = document.querySelectorAll('video.x-mejs')

  window.addEventListener('resize', () => {
    for(let video of videos) {
      video.dispatchEvent(new Event('playing'))
    }
  })
})

Hello Jonathan,

Thanks for sharing this nifty little script. More and more people will be happy about this information.

Cheers.

I added that code in the JS and it still did not fix the issue for the video to play on an Iphone.

Hey Mary,

We do not provide support for user or 3rd party custom codes. Jonathan, just shared what works for him.

Please reach out to a developer or if you want us to receive custom coding guidance, consider subscribing to our One Premium Support.

Thanks.