{"id":677,"date":"2014-08-31T08:47:46","date_gmt":"2014-08-31T08:47:46","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=677"},"modified":"2015-08-10T07:11:41","modified_gmt":"2015-08-10T07:11:41","slug":"wait-for-images-to-load-and-then-execute-all-other-code","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/wait-for-images-to-load-and-then-execute-all-other-code\/","title":{"rendered":"Wait for images to load and then execute all other code"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[677]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>So, <a href=\"http:\/\/stackoverflow.com\/questions\/5869894\/wait-for-images-to-load-and-then-execute-all-other-code\">my question<\/a>\u00a0was\u00a0as follows:<\/p>\n<p>What I want to do is: wait until all images are preloaded and only then execute all other javascript code. As far as I&#8217;m concerned it can (but not a must) have a &#8220;loading&#8230;&#8221; message.<\/p>\n<p>The fact is that I have a pretty big image in the body background and 2 other images which are also bigger, and so I would like to preload them so that then they would show instantly and would not have that ugly &#8220;loading&#8221; image effect.<\/p>\n<p>This is what I&#8217;m using now but it&#8217;s not good:<\/p>\n<pre class=\"lang:default decode:true \">$(document).ready(function()\r\n{    \r\n     preloadImages();\r\n     ...some other code...\r\n\r\n    function preloadImages(){\r\n         imagePreloader([\r\n            'images\/1.png',\r\n            'images\/2.jpg',\r\n            'images\/3.png',                \r\n         ]);\r\n     } \r\n\r\n     function imagePreloader(arrayOfImages) {\r\n         $(arrayOfImages).each(function(){\r\n             (new Image()).src = this;\r\n         });\r\n     }   \r\n}<\/pre>\n<p>I don&#8217;t know, maybe I should call this preloader somewhere out of the .ready()? or something like that?\u00a0Btw, yes, I also read\u00a0<a style=\"color: #4a6b82;\" href=\"http:\/\/stackoverflow.com\/questions\/544993\/official-way-to-ask-jquery-wait-for-all-images-to-load-before-executing-something\">this<\/a>\u00a0post and I don&#8217;t know why but .ready() works faster for me.<\/p>\n<p>The answer, by user Kevin Ennis, was this:<\/p>\n<blockquote><p>Instead of trying to preload, you could just execute your script on&#8230;<\/p>\n<pre class=\"default prettyprint prettyprinted\"><code><span class=\"pln\">window<\/span><span class=\"pun\">.<\/span><span class=\"pln\">onload <\/span><span class=\"pun\">=<\/span><span class=\"kwd\" style=\"color: #00008b;\">function<\/span><span class=\"pun\">(){..}<\/span><\/code><\/pre>\n<p>That doesn&#8217;t fire until all images have been loaded.<\/p><\/blockquote>\n<p>My own solution was this:<\/p>\n<blockquote>\n<p style=\"color: #000000;\">Ok, so finally I got this thing to work. My problem? I was setting the waiting div wrong. This is my code now: I have the loading div which I show above everything and then when all images load (using $(window).load(function(){&#8230;}); as suggested I, hide that div.<\/p>\n<pre class=\"default prettyprint prettyprinted\" style=\"color: #000000;\"><code><span class=\"tag\" style=\"color: #800000;\">&lt;div<\/span><span class=\"atn\" style=\"color: #ff0000;\">id<\/span><span class=\"pun\">=<\/span><span class=\"atv\" style=\"color: #0000ff;\">\"loading\"<\/span><span class=\"tag\" style=\"color: #800000;\">&gt;<\/span><span class=\"tag\" style=\"color: #800000;\">&lt;div<\/span><span class=\"atn\" style=\"color: #ff0000;\">id<\/span><span class=\"pun\">=<\/span><span class=\"atv\" style=\"color: #0000ff;\">\"waiting\"<\/span><span class=\"tag\" style=\"color: #800000;\">&gt;<\/span><span class=\"tag\" style=\"color: #800000;\">&lt;img<\/span><span class=\"atn\" style=\"color: #ff0000;\">class<\/span><span class=\"pun\">=<\/span><span class=\"atv\" style=\"color: #0000ff;\">\"loadingGif\"<\/span><span class=\"atn\" style=\"color: #ff0000;\">src<\/span><span class=\"pun\">=<\/span><span class=\"atv\" style=\"color: #0000ff;\">\"images\/loading.gif\"<\/span><span class=\"tag\" style=\"color: #800000;\">&gt;<\/span><span class=\"tag\" style=\"color: #800000;\">&lt;\/div&gt;<\/span><span class=\"tag\" style=\"color: #800000;\">&lt;\/div&gt;<\/span><span class=\"pln\">\r\n\r\n#loading\r\n{\r\n   background-size: 100%;\r\n   background-color:#000;\r\n   width: 100%;\r\n   height: 100%;\r\n   margin: 0px;\r\n   padding: 0px;\r\n   z-index:999;\r\n}  \r\n\r\n#waiting\r\n{\r\n   margin-left: auto;\r\n   margin-right: auto;    \r\n   position:absolute;\r\n   top: 39%;\r\n   left: 27.81%;\r\n   width: 45%;\r\n   height: 150px;    \r\n   background-color: #FFF;\r\n   border: 12px solid #FF771C;\r\n   text-align: center;\r\n}<\/span><\/code><\/pre>\n<p style=\"color: #000000;\">And my jQuery code is this:<\/p>\n<pre class=\"default prettyprint prettyprinted\" style=\"color: #000000;\"><code><span class=\"pln\">$<\/span><span class=\"pun\">(<\/span><span class=\"pln\">window<\/span><span class=\"pun\">).<\/span><span class=\"pln\">load<\/span><span class=\"pun\">(<\/span><span class=\"kwd\" style=\"color: #00008b;\">function<\/span><span class=\"pun\">()<\/span><span class=\"pun\">{<\/span><span class=\"pln\">    \r\n    $<\/span><span class=\"pun\">(<\/span><span class=\"str\" style=\"color: #800000;\">'#loading'<\/span><span class=\"pun\">).<\/span><span class=\"pln\">addClass<\/span><span class=\"pun\">(<\/span><span class=\"str\" style=\"color: #800000;\">'hidden'<\/span><span class=\"pun\">);<\/span><span class=\"pun\">...<\/span><span class=\"pun\">}<\/span><\/code><\/pre>\n<\/blockquote>\n<p>Additionally, user Alex had a great answer too:<\/p>\n<blockquote><p>I\u00a0\u00a0have a plugin named\u00a0<a style=\"color: #4a6b82;\" href=\"https:\/\/github.com\/alexanderdickson\/waitForImages\">waitForImages<\/a>\u00a0that lets you attach a callback when descendent images have loaded.<\/p>\n<p style=\"color: #000000;\">In your case, if you wanted to wait for all assets to download,\u00a0<code>$(window).load()<\/code>\u00a0may be fine. But you could do it a bit cooler with my plugin \ud83d\ude42<\/p>\n<pre class=\"default prettyprint prettyprinted\" style=\"color: #000000;\"><code><span class=\"kwd\" style=\"color: #00008b;\">var<\/span><span class=\"pln\"> loading <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> $<\/span><span class=\"pun\">(<\/span><span class=\"str\" style=\"color: #800000;\">'#loading'<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\r\n\r\n$<\/span><span class=\"pun\">(<\/span><span class=\"str\" style=\"color: #800000;\">'body'<\/span><span class=\"pun\">).<\/span><span class=\"pln\">waitForImages<\/span><span class=\"pun\">(<\/span><span class=\"kwd\" style=\"color: #00008b;\">function<\/span><span class=\"pun\">()<\/span><span class=\"pun\">{<\/span><span class=\"pln\">    \r\n    loading<\/span><span class=\"pun\">.<\/span><span class=\"pln\">addClass<\/span><span class=\"pun\">(<\/span><span class=\"str\" style=\"color: #800000;\">'hidden'<\/span><span class=\"pun\">);<\/span><span class=\"pun\">},<\/span><span class=\"kwd\" style=\"color: #00008b;\">function<\/span><span class=\"pun\">(<\/span><span class=\"pln\">loaded<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> total<\/span><span class=\"pun\">)<\/span><span class=\"pun\">{<\/span><span class=\"pln\">\r\n    loading<\/span><span class=\"pun\">.<\/span><span class=\"pln\">html<\/span><span class=\"pun\">(<\/span><span class=\"pln\">loaded <\/span><span class=\"pun\">+<\/span><span class=\"str\" style=\"color: #800000;\">' of '<\/span><span class=\"pun\">+<\/span><span class=\"pln\"> total<\/span><span class=\"pun\">);<\/span><span class=\"pun\">});<\/span><\/code><\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=677"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/677\/revisions"}],"predecessor-version":[{"id":1714,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/677\/revisions\/1714"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}