user1937021
user1937021

Reputation: 10791

show html5 videos only on desktop

I have this code which populates the src attributes of the sources if the user is not on mobile:

 if($.browser.mobile)
   {
       console.log("is mobile")
      // it is mobile browser
   }
   else
   {
     console.log("is desktop")
      // no mobile browser
      var sources = document.querySelectorAll('video#patient-video source');
     // Define the video object this source is contained inside
     var video = document.querySelector('video#patient-video');
     for(var i = 0; i<sources.length;i++) {
       sources[i].setAttribute('src', sources[i].getAttribute('data-src'));
     }
     // If for some reason we do want to load the video after, for desktop as opposed to mobile (I'd imagine), use videojs API to load
     video.load();
     video.muted= "muted";
   }

To check if it's a mobile browser, I use the plugin:

detectmobilebrowser.js

My HTML is as follows:

 <video id="patient-video" width="100%" preload="none" poster="../../assets/img/patient-home-1600.jpg" autoplay loop>
            <source data-src="../../assets/video/patient-home-video-comp.mp4" src="" type="video/mp4">
            <source data-src="../../assets/video/patient-home-video-comp.webm" src="" type="video/webm">
            <source data-src="../../assets/video/patient-home-video-comp.ogv" src="" type="video/ogg">
          </video>

This works fine for one specific video, but how can I adapt the Javscript so that it does the same for all videos on the page.

Upvotes: 0

Views: 5321

Answers (1)

Rachel Gallen
Rachel Gallen

Reputation: 28563

You should use a class not an id if you are using multiple vids of the same name

if($.browser.mobile)
   {
       console.log("is mobile")
      // it is mobile browser
   }
   else
   {
     console.log("is desktop")
      // no mobile browser
      var sources = document.querySelectorAll('video.patient-video source');
     // Define the video object this source is contained inside
     var video = document.querySelector('video.patient-video');
     for(var i = 0; i<sources.length;i++) {
       sources[i].setAttribute('src', sources[i].getAttribute('data-src'));
     }
     // If for some reason we do want to load the video after, for desktop as opposed to mobile (I'd imagine), use videojs API to load
     video.load();
     video.muted= "muted";
   }

note the dot instead of the hash tag. You don't even need the 'video' before it. Don't forget to update your html accordingly

it should be

<video class="patient-video" width="100%" preload="none" poster="../../assets/img/patient-home-1600.jpg" autoplay loop>
            <source data-src="../../assets/video/patient-home-video-comp.mp4" src="" type="video/mp4">
            <source data-src="../../assets/video/patient-home-video-comp.webm" src="" type="video/webm">
            <source data-src="../../assets/video/patient-home-video-comp.ogv" src="" type="video/ogg">
          </video>

Upvotes: 6

Related Questions