I am building a website and wish to serve a video if the user is on a desktop device, and if on a mobile I am going to serve a gif.
Does anyone have any guidance for best practices on this?
Is the below code enough? When I test this on chrome it doesn't show the mobile gif, but perhaps something is incorrect here.
Should I be using modernizr to cover browser/device inconsistencies I may not be aware of? Is there a data-src
approach that I should be taking perhaps?
<div id="main"></div>
<script type="text/javascript">
var main = document.getElementById('main');
//check if a mobile device
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
main.innerHTML = '<img src="http://static1.squarespace.com/static/552a5cc4e4b059a56a050501/565f6b57e4b0d9b44ab87107/566024f5e4b0354e5b79dd24/1449141991793/NYCGifathon12.gif>"';
}
else {
main.innerHTML = '<video width="640" height="360" controls="" autoplay=""><source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/mp4"><source src="http://clips.vorwaerts-gmbh.de/VfE.webm" type="video/webm"><source src="http://clips.vorwaerts-gmbh.de/VfE.ogv" type="video/ogg"></video>';
}
</script>