Prevent HTML5 videos from downloading the files on mobile - videojs
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.
Disable background video in resource constrained environments
The answer seems to be that no, there is no standard for this today.
(If you're reading his from the future, and this has changed, please post an answer!)
Stop video from loading in background on mobile
Assuming you're setting visibility via CSS, or in earlier JS code, you can do something like this:
- Include
data-src
attributes instead ofsrc
on your video elements. No src to load if we don't need to. - If the video is visible, copy
data-src
tosrc
- Now call
.load()
on thevideo
element, to pick up the new values.
$( function() { const bgv = $('#bgvid');
if (bgv.is(':visible')) { $('source', bgv).each( function() { const el = $(this); el.attr('src', el.data('src')); } );
bgv[0].load(); } })
.hidden-xs { display: none;}
/* dummy criterion for demo purposes */@media screen and (min-width: 300px) { .hidden-xs { display: block; }}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<video id="bgvid" class="hidden-xs " controls> <source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4"> <source type="video/ogg" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2.ogv"></video>
Prevent mobile users from downloading desktop carousel images
Media Queries will perfectly solve this issue.
/* Mobile-first */
element {
background-image:url("small image here");
}
/* Desktop */
@media screen and (min-width: 1024px) {
element {
background-image:url("large image here");
}
}
If you use this code and have your browser at a size greater than 1024px and using the developer tools, inspect the element, you will see the large version of the image being used as the source for the background. If you then shrink your browser below 1024, you will see the source change to the small version. Browsers will only download the value of the source for the image, so a mobile user will never wind up downloading the large version.
Related Topics
How to Wait for Element to Disappear in Cypress
Jquery Click Events Firing Multiple Times
How to Export Json Data to Excel File Using JavaScript
Slick Carousel - Force Slides to Have the Same Height
Html5 and JavaScript to Play Videos Only When Visible
Calculate and Display % Discount for Price Classes JavaScript
How to Redirect to Another Page Using Angularjs
Reverse Strings Without Changing the Order of Words in a Sentence
Why Is the Onclick Event Triggered Twice
Why Flatlist Is Not Updating Dynamically in React Native
Jquery Get Closest Td Text Value
Launch Chrome Browser from Internet Explorer
How to Add Pm or Am Based on Hour from Input
How to Only Remove the "Watch Later" and "Share" Buttons from Youtube Iframe Embed Player
Useselector Not Updating When Store Has Changed in Reducer. Reactjs Redux