Created
March 28, 2012 09:19
-
-
Save jcayzac/2224992 to your computer and use it in GitHub Desktop.
Responsive loading of iframes (e.g. youtube embeds)
This file contains hidden or 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
<h1>This is loaded only after the DOM is ready, if JS is supported</h1> | |
<p>Loading the iframe normally would block until the iframe's own DOM is ready, resulting in slow loading times. Non-Javascript browsers get normal (inefficient) behaviour.</p> | |
<!-- note: on JS-enabled browsers, the noscript element will get replaced by its content (keep that in mind for CSS) --> | |
<noscript class="noblock"> | |
<iframe src="https://www.youtube-nocookie.com/embed/g2FOLrC2e6E?wmode=opaque&vq=hd720&autohide=1&fs=1&iv_load_policy=3&loop=1&rel=0&showsearch=0&showinfo=0&modestbranding=1"></iframe> | |
</noscript> |
This file contains hidden or 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
// in your DOMContentLoaded handler: | |
Array.prototype.forEach.call(document.querySelectorAll("noscript.noblock"), function(noscript) { | |
var parent = noscript.parentElement || noscript.parentNode, | |
div = document.createElement("div") | |
div.innerHTML = noscript.innerText || noscript.textContent | |
while (typeof div.childNodes[0] !== "undefined") | |
parent.insertBefore(div.removeChild(div.childNodes[0]), noscript) | |
parent.removeChild(noscript) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment