动态加载JS文件

官方案例中的资源文件是在切换场景的时候动态加载进来的,但JS文件则是在启动的时候一起加载进来的,我想问一下JS文件能不能在需要的时候再加载?

Hi, @ying1248

For now we have no open API to do this, but you can do it with standard HTML request, here is some sample code

function appendJSFile(url, loadedCallback) {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    var self = this;
    script.addEventListener('load',function(){
        loadedCallback();
        this.removeEventListener('load', arguments.callee, false);
    }, false);
    document.body.appendChild(script);
}

Huabin

thank you for your help