DOMContentLoaded
is the first thing that is executed.
BUT
if i place an image like this in the html. and this image is cached then the image onload executes before DOMContentLoaded
.
html
<img onload="console.log('imgLoaded');" src="http://placekitten.com/16/16">
js
window.addEventListener('DOMContentLoaded',function(){
console.log('DOMContentLoaded')
},false);
DEMO
http://jsfiddle.net/yE9qU/
output:
- DOMContentLoaded
- imgLoaded
- imgLoaded <----- wrong
- DOMContentLoaded
also putting the onload event directly on the image does not work.
http://jsfiddle.net/yE9qU/1/
output:
- 10 10
- 10 10
- 16 16 <----- wrong
- 10 10
This means that there is no way to get the right css assigned size before everything loads... so after window.onload
Solutions...
depends on what you need.
if there are not to many images i would add them after executing load
or DOMContentLoaded
.
like you say putting the script at the bottom of the page. but not so sure if image is already cached.
a. do the math after everything has loaded (it's just some milliseconds)
b. hide the images until math is done.
If you explain exactly why you need the size of the images in that exact moment it's easier for us to find you a proper alternative solution.