Skip to content

Instantly share code, notes, and snippets.

@beiweiqiang
Created March 10, 2019 03:27
Show Gist options
  • Save beiweiqiang/247cfea1ff773f45256a7cbcea61349a to your computer and use it in GitHub Desktop.
Save beiweiqiang/247cfea1ff773f45256a7cbcea61349a to your computer and use it in GitHub Desktop.
document 上的 DOMContentLoaded 事件 / window 上的 load 事件
<!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>
</head>
<body>
<div>
123123
</div>
<img id="img" src="https://via.placeholder.com/1500" alt="img">
<script>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment