Window Events in JavaScript
Handle window and page events
IntermediateEvents ProgramsExample 8 of 10
window-events.js
1// Method 1: Load event2window.addEventListener('load', function() {3 console.log('Page fully loaded');4 // All resources loaded5});67// Method 2: DOMContentLoaded (faster)8document.addEventListener('DOMContentLoaded', function() {9 console.log('DOM ready');10 // DOM ready, images may still load11});1213// Method 3: Before unload14window.addEventListener('beforeunload', function(e) {15 e.preventDefault();16 e.returnValue = ''; // Chrome requires returnValue17 return ''; // Some browsers18 // Shows confirmation dialog19});2021// Method 4: Unload22window.addEventListener('unload', function() {23 console.log('Page unloading');24 // Cleanup code25});2627// Method 5: Resize28window.addEventListener('resize', function() {29 console.log('Window resized');30 console.log('Width:', window.innerWidth);31 console.log('Height:', window.innerHeight);32});3334// Method 6: Scroll35window.addEventListener('scroll', function() {36 console.log('Scrolled');37 console.log('Scroll Y:', window.scrollY);38 console.log('Scroll X:', window.scrollX);39});4041// Method 7: Scroll with throttling42let scrollTimeout;43window.addEventListener('scroll', function() {44 clearTimeout(scrollTimeout);45 scrollTimeout = setTimeout(function() {46 console.log('Scroll ended');47 }, 150);48});4950// Method 8: Visibility change51document.addEventListener('visibilitychange', function() {52 if (document.hidden) {53 console.log('Page hidden');54 } else {55 console.log('Page visible');56 }57});5859// Method 9: Online/Offline60window.addEventListener('online', function() {61 console.log('Connection restored');62});6364window.addEventListener('offline', function() {65 console.log('Connection lost');66});6768// Method 10: Focus/Blur69window.addEventListener('focus', function() {70 console.log('Window focused');71});7273window.addEventListener('blur', function() {74 console.log('Window blurred');75});7677// Method 11: Hash change (URL fragment)78window.addEventListener('hashchange', function() {79 console.log('Hash changed:', window.location.hash);80});8182// Method 12: Popstate (back/forward)83window.addEventListener('popstate', function(e) {84 console.log('History changed');85 console.log('State:', e.state);86});8788// Method 13: Error handling89window.addEventListener('error', function(e) {90 console.error('Global error:', e.message);91 console.error('File:', e.filename);92 console.error('Line:', e.lineno);93});9495window.addEventListener('unhandledrejection', function(e) {96 console.error('Unhandled promise rejection:', e.reason);97});
Output
// Output depends on window events
What's going on
Window events handle page lifecycle.
Load Events
load: All resources loaded
DOMContentLoaded: DOM ready (faster)
beforeunload: Before page unloads
unload: Page unloading
Window Events
resize: Window resized
scroll: Page scrolled
focus/blur: Window focus
Page Visibility
visibilitychange: Tab visibility
hidden: Page hidden
visible: Page visible
Network Events
online: Connection restored
offline: Connection lost
Navigation Events
hashchange: URL hash changed
popstate: History navigation
Error Events
error: Global errors
unhandledrejection: Promise errors
Use Cases
Page initialization
Cleanup on exit
Responsive design
Analytics tracking