{"id":177,"date":"2006-06-07T15:49:04","date_gmt":"2006-06-07T13:49:04","guid":{"rendered":"http:\/\/www.maratz.com\/blog\/archives\/2006\/06\/07\/preload-images-with-javascript\/"},"modified":"2013-01-15T19:06:40","modified_gmt":"2013-01-15T18:06:40","slug":"preload-images-with-javascript","status":"publish","type":"post","link":"http:\/\/www.maratz.com\/blog\/archives\/2006\/06\/07\/preload-images-with-javascript\/","title":{"rendered":"Preload images with JavaScript"},"content":{"rendered":"<p><a href=\"http:\/\/web.burza.hr\/en\/contact\/#content\"><img loading=\"lazy\" src=\"\/img\/sshots\/burza_button.gif\" height=\"105\" width=\"420\" alt=\"web.burza contact form button\" \/><\/a><\/p>\n<p>Last week I received an e-mail form a guy asking about how to preload interface images for submit button in forms, like the one at the <a href=\"http:\/\/web.burza.hr\/en\/contact\/#content\">web.burza contact form<\/a>. The method is quite simple, really.<!--more--><\/p>\n<p>Here\u2019s the sample part of the HTML form from the web.burza site:<\/p>\n<pre><code>&#60;form method=\"post\" action=\"#\">\r\n    &#60;fieldset>\r\n        &#60;label for=\"email\" class=\"required\">E-mail&#60;\/label>\r\n        &#60;input type=\"text\" size=\"30\" name=\"email\" id=\"email\" \/>\r\n        &#60;input type=\"image\" id=\"submit_button\" src=\"redButtonOff.gif\" alt=\"[Send]\" title=\"Send message\" \/>\r\n    &#60;\/fieldset>\r\n&#60;\/form><\/code><\/pre>\n<p>And the code placed in the external JavaScript file:<\/p>\n<pre><code>&#60;script type=\"text\/javascript\">\r\n\/* &#60;[CDATA[ *\/\r\nvar button_on = new Image();\r\nvar button_go = new Image();\r\nbutton_on.src = 'redButtonOn.gif';\r\nbutton_go.src = 'redButtonGo.gif';\r\nonload = function() {\r\n    var sb = document.getElementById('submit_button');\r\n    if(sb) {\r\n        sb.onmouseover = function() { this.src='redButtonOn.gif'; }\r\n        sb.onmouseout = function() { this.src='redButtonOff.gif'; }\r\n        sb.onfocus = function() { this.src='redButtonOn.gif'; }\r\n        sb.onblur = function() { this.src='redButtonOff.gif'; }\r\n        sb.onclick = function() { this.src='redButtonGo.gif'; }\r\n    };\r\n};\r\n\/* ]]> *\/\r\n&#60;\/script><\/code><\/pre>\n<p>JavaScript experts usually advise not to use HTML-DOM properties, such as <code>src<\/code>. Instead, we should create images with a \u2018true\u2019 DOM (a.k.a. DOM <em>Core<\/em>), and set the <code>src<\/code> attribute with the <code>setAttribute<\/code> method. The difference between the two is that the DOM Core methods can be used by any programming language with DOM support, not just JavaScript, but that\u2019s another story.<\/p>\n<p>See the following snippet:<\/p>\n<pre><code>&#60;script type=\"text\/javascript\">\r\n\/* &#60;[CDATA[ *\/\r\nvar button_on = document.createElement('img');\r\nvar button_go = document.createElement('img');\r\nbutton_on.setAttribute('src','redButtonOn.gif');\r\nbutton_go.setAttribute('src','redButtonGo.gif');\r\nonload = function() {\r\n    var sb = document.getElementById('submit_button');\r\n    if(sb) {\r\n        sb.onmouseover = function() { this.src='redButtonOn.gif'; }\r\n        sb.onmouseout = function() { this.src='redButtonOff.gif'; }\r\n        sb.onfocus = function() { this.src='redButtonOn.gif'; }\r\n        sb.onblur = function() { this.src='redButtonOff.gif'; }\r\n        sb.onclick = function() { this.src='redButtonGo.gif'; }\r\n    };\r\n};\r\n\/* ]]> *\/\r\n&#60;\/script><\/code><\/pre>\n<p>Okay, that was for the single form button, with just two images. But what about the rest of it? Say, you have the AJAX magic somewhere on the site, which pretty quickly delivers data into the not so fast newly created layout element with all those additional fancy supporting imagery. See the example on the <a href=\"http:\/\/www.hellgatelondon.com\/demons\/\">Hellgate: London demon listing page<\/a>\u2026<\/p>\n<p><a href=\"http:\/\/www.hellgatelondon.com\/demons\/\"><img loading=\"lazy\" class=\"alignleft\" src=\"\/img\/sshots\/hellgate_demon_preview.gif\" width=\"420\" height=\"105\" alt=\"Hellgate: London demon preview\" \/><\/a><\/p>\n<p>No worries \u2014 for multiple image preload, we\u2019ll just loop all the image sources with a <code>for<\/code> loop. And here\u2019s one possible solution how to handle this:<\/p>\n<pre><code>var preloaded = new Array();\r\nfunction preload_images() {\r\n    for (var i = 0; i < arguments.length; i++){\r\n        preloaded[i] = document.createElement('img');\r\n        preloaded[i].setAttribute('src',arguments[i]);\r\n    };    \r\n};\r\npreload_images(\r\n    '\/css\/cssimg\/item_listing\/vli_top.gif',\r\n    '\/css\/cssimg\/item_listing\/vli_bottom.gif',\r\n    '\/css\/cssimg\/item_listing\/vli_active_top.gif',\r\n    '\/css\/cssimg\/item_listing\/vli_active_bottom.gif',\r\n    '\/css\/cssimg\/main_layout\/plasma_box_gallery_top_demon.gif'\r\n);<\/code><\/code><\/pre>\n<p>The above code is best placed in the first external JavaScript file.<\/p>\n<h3>Suggestion<\/h3>\n<p><a href=\"http:\/\/www.hellgatelondon.com\/demons\/#search_filter\"><img loading=\"lazy\" class=\"alignleft\" src=\"\/img\/sshots\/hellgate_checkboxes.gif\" width=\"210\" height=\"110\" alt=\"Hellgate: London checkboxes\" style=\"margin-bottom: 20px;\" \/><\/a> My personal favorite images for the preload array are favicon.ico and the footer.gif, but also <a href=\"\/blog\/archives\/2006\/06\/11\/fancy-checkboxes-and-radio-buttons\/\">fancy checkboxes and radio buttons<\/a> imagery.<\/p>\n<h3 style=\"clear: both;\">Related posts<\/h3>\n<ul>\n<li><a href=\"\/blog\/archives\/2005\/06\/22\/preload-hover-images-in-css\/\">Preload :hover images<\/a><\/li>\n<li><a href=\"\/blog\/archives\/2005\/05\/02\/image-preloader\/\">Image preloader (visual effect)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Preload all those interface states<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[25,52],"_links":{"self":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/177"}],"collection":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/comments?post=177"}],"version-history":[{"count":0,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/177\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/media?parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/categories?post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/tags?post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}