Adapting coffeescript to use window.onload

I have the following coffeescript file that works, except for the fact that when the images take some time, the download is done before the images are loaded, and then it does not have the desired effect:

ready = -> jQuery -> $('.box').find('img').each -> imgClass = if @width / @height > 1 then 'wide' else 'tall' $(this).addClass imgClass return return $(document).ready(ready) $(document).on('page:load', ready) 

How to run my function only after loading the whole window?

My best attempt so far looks like

 window.onload = -> $('.box').find('img').each -> imgClass = if @width / @height > 1 then 'wide' else 'tall' $(this).addClass imgClass return return 

but that will not work. I also tried several other possibilities, but I can not understand what is wrong.

My question is different from this , because I do not want to wait until all the coffee houses are completed, I want to wait until all the images in my web page are loaded.

+6
source share
1 answer

To execute the required code AFTER all the images are loaded, use the jQuery load() method in the window . document looking at the DOM, and window is watching the content.

In coffee, it will look like this:

 $(window).load -> alert 'All images are loaded' 

The difference between this and what you had is using window.onload , while I suggest $(window).load() .

+3
source

All Articles