Last active
April 15, 2020 15:38
-
-
Save JamesIgoe/1ae15c293cd9b5df5687b6e49f46082c to your computer and use it in GitHub Desktop.
Javscript to hide HTML5 Video element from IE, as it cannot display it, and instead displays a link only,, Source: https://stackoverflow.com/a/55611789/3272594
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Styles.Render("~/Content/css") | |
@Scripts.Render("~/bundles/modernizr") | |
@Scripts.Render("~/bundles/jquery") | |
<style> | |
</style> | |
<script> | |
(function () { | |
"use strict"; | |
function insertAfter(newNode, referenceNode) { | |
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); | |
} | |
function setVideoFallback(lazyArea) { | |
var lowData = false; | |
if ("connection" in navigator) { | |
lowData = navigator.connection.saveData === true || | |
navigator.connection.effectiveType === "slow-2g" || | |
navigator.connection.effectiveType === "2g"; | |
} | |
//DocumentFragments don't support getElementsByTagName | |
//oldIE doesn't support querySelectorAll | |
var lazyVideos = lazyArea.querySelectorAll ? lazyArea.querySelectorAll("video") : lazyArea.getElementsByTagName("video"); | |
for (var i = lazyVideos.length; i--;) { | |
var lazyVideo = lazyVideos[i]; | |
var cantPlay = true; | |
if (lazyVideo.canPlayType) { | |
//Loop through the various source elements, and check if | |
//the browser thinks it can play them | |
//This works better if we specify the codec along with | |
//the MIME type | |
var sources = lazyVideo.getElementsByTagName("source"); | |
for (var i2 = sources.length; i2--;) { | |
if (lazyVideo.canPlayType(sources[i2].type)) { | |
cantPlay = false; | |
break; | |
} | |
} | |
} | |
//If on a low-data connection, remove the autoplay attribute | |
//(it's only polite) | |
if (lowData) { | |
lazyVideo.removeAttribute("autoplay"); | |
lazyVideo.setAttribute("controls", ""); | |
} | |
//If you can't play any of the available formats, skip straight to fallback content | |
if (cantPlay) { | |
//Extract the fallback and replace the video with it | |
var children = lazyVideo.childNodes; | |
for (var i3 = children.length; i3--;) { | |
var childNode = children[i3]; | |
if (childNode.tagName !== "TRACK" && childNode.tagName !== "SOURCE") { | |
insertAfter(childNode, lazyVideo); | |
} | |
} | |
lazyVideo.parentNode.removeChild(lazyVideo); | |
} | |
} | |
} | |
/** | |
* Retrieve the elements from the 'lazy load' noscript tags and prepare them for display | |
*/ | |
function setUp() { | |
//Get all the noscript tags on the page | |
var lazyLoadAreas = document.getElementsByTagName("noscript"); | |
var supportsTemplates = typeof HTMLTemplateElement === "function"; | |
for (var i = lazyLoadAreas.length; i--;) { | |
var noScriptTag = lazyLoadAreas[i]; | |
//only process the ones marked for lazy loading | |
if (!noScriptTag.hasAttribute("data-lazy-load")) continue; | |
// The contents of a noscript tag are treated as text to JavaScript | |
var lazyAreaHtml = noScriptTag.textContent || noScriptTag.innerHTML; | |
// So we stick them in the innerHTML of a new div tag to 'load' them | |
var lazyArea; | |
if (supportsTemplates) { | |
//(if possible, templates are better as they won't start any network calls) | |
var lazyTemplate = document.createElement("template"); | |
lazyTemplate.innerHTML = lazyAreaHtml; | |
lazyArea = lazyTemplate.content; | |
} else { | |
lazyArea = document.createElement("div"); | |
lazyArea.innerHTML = lazyAreaHtml; | |
} | |
setVideoFallback(lazyArea); | |
noScriptTag.parentNode.replaceChild(lazyArea, noScriptTag); | |
} | |
} | |
//If the page has loaded already, run setup - if it hasn't, run as soon as it has. | |
if (document.readyState !== "loading") { | |
setUp(); | |
} else { | |
document.addEventListener("DOMContentLoaded", setUp); | |
} | |
})(); | |
</script> | |
<section> | |
<main> | |
<figure> | |
<noscript data-lazy-load> | |
<video controls autoplay> | |
<source src="@Url.Content("~/Videos/SomeVideo.webm")" type="video/webm; codecs=vp8,vorbis"> | |
<source src="@Url.Content("~/Videos/SomeVideo.mp4")" type="video/mp4. codecs=mp4"> | |
</video> | |
</noscript> | |
<figcaption> | |
<br /> | |
<a href="~/Videos/SomeVideo.mp4" target="_blank" title="Installing Microsoft Outlook on an iPhone"> | |
Installing Microsoft Outlook on an iPhone | |
</a> | |
</figcaption> | |
</figure> | |
</main> | |
</section> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment