Ayxan Əmiraslanlı Ayxan Əmiraslanlı - 3 months ago 10
Javascript Question

javascript add script dynamicly end of page

I want to add script after all scripts loaded. My code is:

var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var get = document.getElementsByTagName("script");
var count = get.length;

var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[count];
s1.async=true;
s1.src='https://embed.tawk.to/57c6c47b2b03647ba16e0e84/default';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();


And I got this error:

Uncaught TypeError: Cannot read property 'parentNode' of undefined


The link is: https://www.alapar.az/

Answer

Substitute

s0=document.getElementsByTagName("script")[count -1] // note `-1`

for

s0=document.getElementsByTagName("script")[count]