Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Last active July 26, 2023 12:27
Show Gist options
  • Save vxhviet/a525c38524a24dbbe4c385405fa1336c to your computer and use it in GitHub Desktop.
Save vxhviet/a525c38524a24dbbe4c385405fa1336c to your computer and use it in GitHub Desktop.

[JavaScript] - Lifecycle DOM Events

document.addEventListener('DOMContentLoaded', function (e) {
  console.log('HTML parsed and DOM tree built!', e);
});

window.addEventListener('load', function (e) {
  console.log('Page fully loaded', e);
});

window.addEventListener('beforeunload', function (e) {
  e.preventDefault();
  console.log(e);
  e.returnValue = ''; // only default generic message in modern browser to prevent abuse
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment