Follow Me

Image preload using javascript

Wed, May 19, 2010

JavaScript

Image preload script using javascript. It is just a simple script that preloads images on loading of the page. It scans images in whole page while loading and assign into the source of image. This script also function for onmouseover and onmouseout, it will change image on mouse over and out, results fast image loading.

Enjoy it :)


<a onmouseover="imgOver();return true;" onmouseout="imgOut();return true;" href="#">
<img border="0" alt="This image changes when you point at it!" width="17" height="15" />
src="example2.gif"&gt;
</a>

In the  section of the page, we have JavaScript code that preloads the image files and defines the event handler functions:

<script language="JavaScript">
if (document.images)
{
    img_on =new Image();  img_on.src ="images/1.gif";
    img_off=new Image();  img_off.src="images/2.gif";
}

function imgOver()
{
    if (document.images) document.imgName.src=img_on.src;
}

function imgOut()
{
    if (document.images) document.imgName.src=img_off.src;
}
</script>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
, , ,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • Leave a Reply