Trying to write Javascript in a loop via YouTube iFrame Embeds

advertisements

I'm trying and failing to write a drop-in YouTube embed player for my users, so that they only have to change one line to change any and all YouTube players on a page. I am dropping this into my page, but nothing is happening.

        <script>
        // Replace line below with video id(s), e.g. ["dQw4w9WgXcQ"] or ["dQw4w9WgXcQ","b1WWpKEPdT4"]
        var ids = ["dQw4w9WgXcQ","b1WWpKEPdT4"];
        //
        //
        var idsAmount = ids.length;
        for (var i in idsAmount) {
            document.write("<iframe src=\"https://www.youtube.com/embed/" + ids[i] + "?modestbranding=1&amp;rel=0&amp;autoplay=1\" /> <br />");
        };
        </script>

No errors in the console. Any idea why nothing happens?

Edit: Here is my final working code:

        <script>
            // Replace line below with video id(s), e.g. ["dQw4w9WgXcQ"] or ["dQw4w9WgXcQ","b1WWpKEPdT4"]
            var ids = ["dQw4w9WgXcQ","b1WWpKEPdT4"];
            //
            if (ids.length == 1) {
                document.write("<iframe width=\"640px\" height=\"360px\" src=\"https://www.youtube.com/embed/" + ids + "?modestbranding=1&rel=0&autoplay=1\" frameborder=\"0\" allowfullscreen></iframe>");
                document.write("<br />");
            }
            else {
                for (var i = 0; i < ids.length; i++) {
                    document.write("<iframe width=\"640px\" height=\"360px\" src=\"https://www.youtube.com/embed/" + ids[i] + "?modestbranding=1&rel=0\" frameborder=\"0\" allowfullscreen></iframe>");
                    document.write("<br /> <br />");
                };
            };
        </script>


First, you are escaping 2 quotes that are not needed to be escaped (for concatenation), also, the loop was wrong, should be as follows:

<script>
    // Replace line below with video id(s), e.g. ["dQw4w9WgXcQ"] or ["dQw4w9WgXcQ","b1WWpKEPdT4"]
    var ids = ["dQw4w9WgXcQ","b1WWpKEPdT4"];
    //
    //
    for (var i = 0; i < ids.length; i++) {
        document.write("<iframe src=\"https://www.youtube.com/embed/" + ids[i] + "?modestbranding=1&amp;rel=0&amp;autoplay=1\" /> <br />");
    };
</script>