javascript动态添加脚本页面结束

时间:2016-08-31 19:54:56

标签: javascript insert element

我想在加载所有脚本后添加脚本。我的代码是:

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);
})();

我收到了这个错误:

Uncaught TypeError: Cannot read property 'parentNode' of undefined

链接是:https://www.alapar.az/

1 个答案:

答案 0 :(得分:0)

替代

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

代表

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