I'm trying to lazy load javascript into the head with this function:
function bfLoadScripts(strPath) {
var r = false;
var scriptTag = document.createElement('script');
scriptTag.src = strPath;
scriptTag.type = 'text/javascript';
scriptTag.addEventListener('load',function() {
//alert('JS loaded');
r = true;
});
var headTag = document.getElementsByTagName('head')[0];
headTag.appendChild(scriptTag);
}
It works in FF (latest), Chrome (latest), IE 11 but not on Safari iOS 5.1 and Safari PC.
I tried this before but it's also not supported in Safari:
scriptTag.onload = scriptTag.onreadystatechange = function() {
if ( !r && (!this.readyState || this.readyState == 'complete') ) {
r = true;
};
Is there a polyfill for the "onload" event? Or asked differently: Is there a total different way of doing this? Without overloading the whole process by using libraries, plugins etc.