Home

Published

- 1 min read

how to trigger events when the document loads in js

img of how to trigger events when the document loads in js

The solution for this is noted below

how to trigger events when the document loads in js

Solution

   document.addEventListener('DOMContentLoaded', ready)
function ready() {
	alert('DOM is ready')

	// image is not yet loaded (unless it was cached), so the size is 0x0
	alert(`Image size: ${img.offsetWidth}x${img.offsetHeight}`)
}

Try other methods by searching on the site. That is if this doesn’t work