• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Preload images with JavaScript ...

This article is talking about how to preload interface images for submit button in forms, like the one at the web.burza contact form.

This article is talking about how to preload interface images for submit button in forms, like the one at the web.burza contact form.

Here’s the sample part of the HTML form from the web.burza site:

<form method="post" action="#">
<fieldset>

<label for="email" class="required">E-mail</label>
<input type="text" size="30" name="email" id="email" />
<input type="image" id="submit_button" src="redButtonOff.gif" alt="[Send]" title="Send message" />
</fieldset>
</form>

And the code placed in the external JavaScript file:

<script type="text/javascript">
/* <[CDATA[ */
var button_on = new Image();
var button_go = new Image();
button_on.src = aredButtonOn.gif;
button_go.src = aredButtonGo.gif;
onload = function() {
var sb = document.getElementById( isubmit_button);
if(sb) {
sb.onmouseover = function() { this.src= aredButtonOn.gif; }
sb.onmouseout = function() { this.src= aredButtonOff.gif; }
sb.onfocus = function() { this.src= aredButtonOn.gif; }
sb.onblur = function() { this.src= aredButtonOff.gif; }
sb.onclick = function() { this.src= aredButtonGo.gif; }
};
};
/* ]]> */
</script>

JavaScript experts advise not to use HTML-DOM properties, such as src. Instead, we should create images with the ‘true’ DOM (a.k.a. DOM Core), and set the src attribute with the setAttribute 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’s another story.

See the following snippet:

<script type="text/javascript">
/* <[CDATA[ */
var button_on = document.createElement(img);
var button_go = document.createElement(img);
button_on.setAttribute( isrc, aredButtonOn.gif);
button_go.setAttribute( isrc, aredButtonGo.gif);
onload = function() {
var sb = document.getElementById( isubmit_button);
if(sb) {
sb.onmouseover = function() { this.src= aredButtonOn.gif; }
sb.onmouseout = function() { this.src= aredButtonOff.gif; }
sb.onfocus = function() { this.src= aredButtonOn.gif; }
sb.onblur = function() { this.src= aredButtonOff.gif; }
sb.onclick = function() { this.src= aredButtonGo.gif; }
};
};
/* ]]> */
</script>

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 Hellgate: London demon listing page…

No worries – for multiple image preload, we’ll just loop all the image sources with a for loop. And here’s one possible solution how to handle this:

var preloaded = new Array();
function preload_images() {
for (var i = 0; i < arguments.length; i++){
preloaded[i] = document.createElement(img);
preloaded[i].setAttribute( isrc,arguments[i]);
};
};
preload_images(
/css/cssimg/item_listing/vli_top.gif,
/css/cssimg/item_listing/vli_bottom.gif,
/css/cssimg/item_listing/vli_active_top.gif,
/css/cssimg/item_listing/vli_active_bottom.gif,
/css/cssimg/main_layout/plasma_box_gallery_top_demon.gif
);

The above code is best placed in the first external JavaScript file.

Original Source: Preload images with JavaScript

 View Full Story.
Posted at 04:15:33 am | Permalink | Posted in Tutorials  Javascript  Image  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Be the first ... |Add your comment.

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


Top Stuff

e-messenger

MessengerFX

eBuddy

ILoveIM

AIM Express

Top 20 Ruby CMS


Our Partners

Facebook Applications

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines

Stock Exchange Chat


About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (111)
  • Articles (85)
  • Bookmarking (35)
  • Calendar (19)
  • Chat (39)
  • ColdFusion (3)
  • CSS (41)
  • Email (23)
  • Facebook (23)
  • Flash (15)
  • Games (6)
  • Google (28)
  • Html (14)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (36)
  • Javascript (171)
  • JSON (21)
  • Perl (2)
  • PHP (88)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (1)
  • Ruby (10)
  • Storage (4)
  • Toolkits (90)
  • Tutorials (199)
  • UI (12)
  • Utilities (167)
  • Web2.0 (13)
  • XmlHttpRequest (20)
  • YUI (4)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange