插入js
function addScript (url, callback) { const script = document.createElement('script') if (script.readyState) { script.onreadystatechange = () => { if (script.readyState === 'loaded' || script.readyState === 'complete') { script.onreadystatechange = '' callback() } } } else { script.onload = () => { callback() } } script.src = url document.head.appendChild(script) }
firefox和opera会按照调用的顺序执行代码,其他浏览器将会按照从服务器返回的顺序执行代码。如果顺序很重要,最好按照顺序合成一个js文件。