欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Ajax技术实现动态加载原生JavaScript文件

最编程 2024-01-15 14:19:44
...

动态加载 JS 文件

var xhr = new XMLHttpRequest;
xhr.open('get','//web.test.com/js/common/jq.js',true);
xhr.onreadystatechange = function(){
    if( xhr.readyState == 4 ){
        if( xhr.status >=200 && xhr.status < 300 || xhr.status == 304 ){
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.text = xhr.responseText;
            document.body.appendChild(script);
        }
    }
};

xhr.send(null);