文件loader.js:
function main() { if (typeof window !== 'undefined') { var script = window.document.createElement('script') script.src = 'https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.min.js' script.onload = function () { console.log('script loaded') } window.onload = function () { console.log('window loaded') } window.document.head.appendChild(script) } else { console.log('window not available yet') } } if (typeof module !== 'undefined' && module.exports) { exports.main = main } main()
文件window.html:
<!DOCTYPE html> <html> <head> <title>Test</title> <script src="loader.js"></script> </head> <body> <div>Test</div> </body> </html>
script loaded window loaded
我的问题:
在上面的代码中,是否保证脚本onload事件总是在窗口onload之前触发?