结论
- 不阻塞dom树解析
- 阻塞dom树渲染
- 阻塞js执行
DOMContentLoaded,html文档加载解析完之后执行
document.addEventListener('DOMContentLoaded',function() {
console.log('DOMContentLoaded')
})
onload,页面所有资源加载完之后执行
window.onload = function() {
console.log('onload')
}
实践
限制network上传下行速度
demo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function() {
console.log('onload')
}
document.addEventListener('DOMContentLoaded',function() {
console.log('DOMContentLoaded')
})
</script>
<link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css" rel="stylesheet">
<script defer>
console.log('hh')
</script>
</head>
<body>
</body>
</html>