admin管理员组文章数量:1391934
I have a hero on a landing page with a video background and want to prevent the webm/mp4 file from being downloaded on mobile devices. I've seen some solutions that involve media queries with display:none
attribute. Even though they're fine at first impression, I verified, using Chrome debug tool connected to my phone, that the file is still being downloaded.
Here's the video presented in the HTML5 markup:
<video preload="metadata" class="hidden-xs" autoplay="autoplay" poster="fallback-image.jpg" loop="loop" id="bgvid">
<source src="video.webm" type="video/webm">
<source src="video.mp4" type="video/mp4">
</video>
The following is the method I use to detect mobile browsers:
function detectmob() {
if( navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
){
// If mobile, then we do all this
}
else {
// If not mobile then do this
}
} // detectmob
How can I prevent someone from downloading the video on mobile devices within my JavaScript function?
I have a hero on a landing page with a video background and want to prevent the webm/mp4 file from being downloaded on mobile devices. I've seen some solutions that involve media queries with display:none
attribute. Even though they're fine at first impression, I verified, using Chrome debug tool connected to my phone, that the file is still being downloaded.
Here's the video presented in the HTML5 markup:
<video preload="metadata" class="hidden-xs" autoplay="autoplay" poster="fallback-image.jpg" loop="loop" id="bgvid">
<source src="video.webm" type="video/webm">
<source src="video.mp4" type="video/mp4">
</video>
The following is the method I use to detect mobile browsers:
function detectmob() {
if( navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
){
// If mobile, then we do all this
}
else {
// If not mobile then do this
}
} // detectmob
How can I prevent someone from downloading the video on mobile devices within my JavaScript function?
Share Improve this question edited Jul 24, 2015 at 14:35 GʀᴜᴍᴘʏCᴀᴛ 9,04820 gold badges90 silver badges160 bronze badges asked Jul 24, 2015 at 14:13 nicozicanicozica 4236 silver badges19 bronze badges 1- could you please share the code block so that it will be helpful for us. – Channaveer Hakari Commented Dec 23, 2019 at 7:43
1 Answer
Reset to default 6Your HTML:
<video preload="metadata" class="hidden-xs" autoplay="autoplay" poster="fallback-image.jpg" loop="loop" id="bgvid">
</video>
Your javascript:
function detectmob() {
if( navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
){
// If mobile, then we do all this
}
else {
// If not mobile then do this
document.getElementById("bgvid").innerHTML = '<source src="video.webm" type="video/webm"><source src="video.mp4" type="video/mp4">';
}
} // detectmob
本文标签: javascriptPrevent a video background from being downloaded on mobile browsersStack Overflow
版权声明:本文标题:javascript - Prevent a video background from being downloaded on mobile browsers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744687398a2619785.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论