Skip to content

Instantly share code, notes, and snippets.

@beiweiqiang
Last active March 10, 2019 03:26
Show Gist options
  • Save beiweiqiang/114dcc063a68e8a97732e30e04181adc to your computer and use it in GitHub Desktop.
Save beiweiqiang/114dcc063a68e8a97732e30e04181adc to your computer and use it in GitHub Desktop.
defer's script 的执行时机
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
document.addEventListener('DOMContentLoaded', function (event) {
console.log('index.html: 9 -> -> ', 'dom');
console.log('index.html: 10 -> -> ', document.getElementById('img').offsetWidth);
});
window.addEventListener('load', function (event) {
console.log('index.html: 13 -> -> ', 'load');
console.log('index.html: 15 -> -> ', document.getElementById('img').offsetWidth);
});
</script>
<script src="index.js" defer></script>
</head>
<body>
<div>
123123
</div>
<img id="img" src="https://via.placeholder.com/1500" alt="img">
<script>
</script>
</body>
</html>
console.log('index.js: 1 -> -> ', 'index');
debugger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment