Append not working? (Vanilla Javascript)

var tag = document.createElement('script');
tag.src = "http://www.youtube.com/iframe_api";
var scriptLength=document.getElementsByTagName('script').length-1;
alert(scriptLength);
var firstScriptTag = document.getElementsByTagName('script')[scriptLength];
console.log(firstScriptTag);
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

This is supposed to append a script near hte end of my page.

<script src="/uploaded/themes/build/js/main.js"></script>
</body>
</html>

These are the last few lines of my page. You see the console log there? In my Javascript? It logs-

<script src="/uploaded/themes/build/js/main.js"></script>

So it is just the append failing. Can anyone spot why?
Page - http ://hawaiip rep.final site.com/stay-con nected

not sure if it’s important, but firstScriptTag is actually the last script tag … at that time.

the insertion does appear though I don’t know if it‘s at the intended place. for me it comes before

    <script>
	    (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
	        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
                                                  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
	        '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer','GTM-P3BGC7');
    </script>

Yeah I’m not going to get in depth, but it’s working now. Basically there was an issue of me calling the YT api to an element I just added an ID to in jQuery so it wasn’t running (needed delegating) but YT api does’t allow for that. Anyway it was a long process but it’s working now. It was appending (the code was working) but there were YT api issues.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.