等待JS加载完成后执行方法

loadScript:function(src, callback) {
            layer.load(2);
            var script = document.createElement('script'),
                head = document.getElementsByTagName('head')[0];
            script.type = 'text/javascript';
            script.charset = 'UTF-8';
            script.src = src;
            if (script.addEventListener) {
                script.addEventListener('load', function () {
                    callback();
                    layer.closeAll('loading');
                }, false);
            } else if (script.attachEvent) {
                script.attachEvent('onreadystatechange', function () {
                    var target = window.event.srcElement;
                    if (target.readyState == 'loaded') {
                        callback();
                        layer.closeAll('loading');
                    }
                });
            }
            head.appendChild(script);
        }

 

你可能感兴趣的:(等待JS加载完成后执行方法)