Q: Can't defer both videos and images on my website. First, english is not my native language and I don't know how to code, so be patient if what I'm writing looks stupid.
I tried to find an answer on Google but I couldn't.
Thank you.

I was trying to defer both the images and the Youtube videos from my website.
Everything worked fine until I tried to do both: if I add the script to defer the image and then the script to defer the video, images show up and the videos on my page don't even load.
This is the script I use to defer images:
<script>
function init() {
var imgDefer = document.getElementsByTagName('img');
for (var i=0; i<imgDefer.length; i++) {
if(imgDefer[i].getAttribute('data-src')) {
imgDefer[i].setAttribute('src',imgDefer[i].getAttribute('data-src'));
} } }
window.onload = init;
</script>

This is the code I use inside my pages to paste the image I want to defer:
<img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" data-src="your-image-here">

This is, instead, the code I use to defer videos:
<script>
function init() {
var vidDefer = document.getElementsByTagName('iframe');
for (var i=0; i<vidDefer.length; i++) {
if(vidDefer[i].getAttribute('data-src')) {
vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));
} } }
window.onload = init;
</script>

Then I paste this to defer: 
<iframe width="560" height="315" src="" data-src="//www.youtube.com/embed/OMOVFvcNfvE" frameborder="0" allowfullscreen></iframe>

They both work if I don't use them together.
The moment I install both the scripts, the images keep getting deferred, while the videos don't even load.
How can I fix this problem?
Thanks in advance and have a good day. :-)
Luca

A: Try this:
function init() {
var imgDefer = document.getElementsByTagName('img');
for (var i = 0; i < imgDefer.length; i++) {
    if (imgDefer[i].getAttribute('data-src')) {
        imgDefer[i].setAttribute('src', imgDefer[i].getAttribute('data-src'));
    }
}


 var vidDefer = document.getElementsByTagName('iframe');
  for (var i = 0; i < vidDefer.length; i++) {
    if (vidDefer[i].getAttribute('data-src')) {
      vidDefer[i].setAttribute('src', vidDefer[i].getAttribute('data-src'));
    }
  }
}
window.onload = init;

