1

I need to fire some javascript after the DOM and CSS is loaded, but not images. I believe that's after document.ready but before window.onload. Anyone know how to achieve this?

I was thinking of loading CSS after document.ready by iterating through document.styleSheets but it feels like a bad idea.

Update: The reason for this unusual requirement is that I need to predict the size which images will be rendered, which is determined by css. Moving the javascript to the bottom of the page solves the issue, but I'd like my script to work when that's not the case too.

Zubin
  • 9,422
  • 7
  • 48
  • 52

3 Answers3

4

You want the DOMContentLoaded event, together with an external script linked after all of the stylesheets.

From the linked page:

if you have a <script> after a <link rel="stylesheet" ...>, the page will not finish parsing - and DOMContentLoaded will not fire - until the stylesheet is loaded.

Razor
  • 27,418
  • 8
  • 53
  • 76
  • http://jsfiddle.net/yE9qU/ does not work.the image load first sometimes.especially if it's cached.but sure DOMContentLoaded is the first thing that executes after the dom. – cocco May 27 '14 at 12:55
  • Thanks but that's not what I'm after. It says "... without waiting for stylesheets... to finish loading" (I need the css). – Zubin May 27 '14 at 13:06
  • @Zubin please double check the quote. It does indeed wait for stylesheets in certain conditions. – Razor May 27 '14 at 13:10
  • Good point @Razor. It works when the script tag is at the bottom of the body (where it should be). I was hoping to get it working wherever the script tag happens to be, but will settle for this. Cheers! – Zubin May 27 '14 at 13:34
1

You could try to use a small image and add the handler to its onload event, see image.onload event and browser cache Add your code with this small image to document.ready.

Please could you elaborate what is your use case, and why putting the code on document.ready event handler would not do the job for you?


Another option would be to attach handlers to onload of each of the style tags, and each of this handlers to increment some counter, which you would check at the same time if the counter = style tags count. (You could determine the count of style tags, the same time you add the event handlers). When the counter has reached the number of style tags, you could fire your custom event, that could be used to have your code run. This however would fail if some of the stylesheets fail to load for some reason, so I would suggest adding a fallback to execute the code on window.onload if the above logic fails.

Community
  • 1
  • 1
Zlatin Zlatev
  • 3,034
  • 1
  • 24
  • 32
1

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.

  1. if there are not to many images i would add them after executing load or DOMContentLoaded.

  2. like you say putting the script at the bottom of the page. but not so sure if image is already cached.

  3. a. do the math after everything has loaded (it's just some milliseconds)

  4. 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.

cocco
  • 16,442
  • 7
  • 62
  • 77