Prevent HTML5 videos from downloading the files on mobile - videojs
One way you could do this is by setting the src
attributes of your video
element via JavaScript, and only doing so based on a media query (using matchMedia
).
This would mean that the bulk of your code would have to move to JavaScript though.
For example, your HTML could be something like:
<video data-mp4="video.mp4" data-webm="video.webm" class="video-js" controls></video>
And then in your JavaScript (pseudo code here, not actual JS):
if (window.matchMedia("(min-width: 640px)").matches) {
// do nothing
} else {
var videos = document.querySelectorAll('.video-js'),
videoFile;
for (var i = 0; i < videos.length; i++) {
// Choose video type
if (video[i].canPlayType("video/mp4") === "probably") {
videoFile = video[i].getAttribute("data-mp4");
}
// do the same check for WebM here...
video[i].src = videoFile;
// Call whatever reload or refresh method video.js has
// for example...
video[i].refresh();
}
}
Something like that might work for you, although you might have to play around a bit with it.
Based on the suggestions Ian kindly made, here is my working solution.
Firstly, I changed each video's child source elements to have an attribute data-src
like so:
<video id="my-id">
<source data-src="somevideo.mp4">
</video>
Then, after performing a mobile check using the script available at http://detectmobilebrowsers.com/ which I modified to include iPads etc (related SO answer here) I simply used the following script to automatically update the src
attribute of each video (if we're on desktop in my case):
var sources = document.querySelectorAll('video#my-id source');
// Define the video object this source is contained inside
var video = document.querySelector('video#my-id');
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();
And that's it! Nothing loads on mobile devices anymore and I can have fairly granular control over the devices it will or won't load on.
Hope this helps somebody.
Based on Ian and GDGR's answers, I modified this to work for multiple videos.
<video class="mobile-no-load">
<source data-src="somevideo.mp4">
</video>
if (window.innerWidth > 730) {
// get multiple videos
var sources = document.querySelectorAll('video.mobile-no-load');
// loop through the videos
sources.forEach(function(source){
// target the src attribute of the <source> element and set it to the data-src attribute
source.childNodes[1].setAttribute('src', source.childNodes[1].getAttribute('data-src'))
});
}