1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Image Preloading

Discussion in 'JavaScript' started by !Unreal, May 6, 2009.

  1. #1
    Ive written an image preloading script but Im not sure if it would work. Could someone look at it and just clear up wether it should work or not

    Code (Text):
    1. <html>
    2. <head>
    3. <script type="text/javascript">
    4. function preloader()
    5. {
    6.     // counter
    7.     var i = 0;
    8.     // create object
    9.     imageObj = new Image();
    10.     // set image list
    11.     images = new Array()
    12.         images[0]="/styles/lightskin/statusicon/pm_forwarded.gif"
    13.     images[1]="/styles/lightskin/statusicon/pm_new.gif"
    14.     images[2]="/styles/lightskin/statusicon/forum_new.gif"
    15.     images[3]="/styles/lightskin/statusicon/wol_lockedout.gif"
    16.     images[4]="/styles/lightskin/statusicon/thread_lock.gif"
    17.     images[5]="/styles/lightskin/statusicon/wol_error.gif"
    18.     images[6]="/styles/lightskin/statusicon/user_online.gif"
    19.     images[7]="/styles/lightskin/statusicon/thread_hot_lock.gif"
    20.     images[8]="/styles/lightskin/statusicon/thread_livetopic.gif"
    21.     images[9]="/styles/lightskin/statusicon/thread_hot_lock_new.gif"
    22.     images[10]="/styles/lightskin/statusicon/thread_hot_new.gif"
    23.     images[11]="/styles/lightskin/statusicon/thread_hot.gif"
    24.     images[12]="/styles/lightskin/statusicon/subforum_old.gif"
    25.     images[13]="/styles/lightskin/statusicon/user_invisible.gif"
    26.     images[14]="/styles/lightskin/statusicon/pm_replied.gif"
    27.     images[15]="/styles/lightskin/statusicon/thread_moved_new.gif"
    28.     images[16]="/styles/lightskin/statusicon/thread_dot_hot_lock.gif"
    29.     images[17]="/styles/lightskin/statusicon/thread_dot.gif"
    30.     images[18]="/styles/lightskin/statusicon/user_offline.gif"
    31.     images[19]="/styles/lightskin/statusicon/forum_link.gif"
    32.     images[20]="/styles/lightskin/statusicon/thread_dot_lock.gif"
    33.     images[21]="/styles/lightskin/statusicon/post_old.gif"
    34.     images[22]="/styles/lightskin/statusicon/thread_dot_new.gif"
    35.     images[23]="/styles/lightskin/statusicon/thread_moved.gif"
    36.     images[24]="/styles/lightskin/statusicon/thread_livetopic.orange.gif"
    37.     images[25]="/styles/lightskin/statusicon/forum_old_lock.gif"
    38.     images[26]="/styles/lightskin/statusicon/thread.gif"
    39.     images[27]="/styles/lightskin/statusicon/pm_old.gif"
    40.     images[28]="/styles/lightskin/statusicon/forum_new_lock.gif"
    41.     images[29]="/styles/lightskin/statusicon/announcement_new.gif"
    42.     images[30]="/styles/lightskin/statusicon/thread_dot_hot.gif"
    43.     images[31]="/styles/lightskin/statusicon/wol_nopermission.gif"
    44.     images[32]="/styles/lightskin/statusicon/announcement_old.gif"
    45.     images[33]="/styles/lightskin/statusicon/forum_old.gif"
    46.     images[34]="/styles/lightskin/statusicon/subforum_new.gif"
    47.     images[35]="/styles/lightskin/statusicon/thread_dot_hot_new.gif"
    48.     images[36]="/styles/lightskin/statusicon/thread_new.gif"
    49.     images[37]="/styles/lightskin/statusicon/post_new.gif"
    50.     images[38]="/styles/lightskin/statusicon/thread_dot_lock_new.gif"
    51.     images[39]="/styles/lightskin/statusicon/subforum_link.gif"
    52.     images[40]="/styles/lightskin/statusicon/thread_lock_new.gif"
    53.     images[41]="/styles/lightskin/statusicon/thread_dot_hot_lock_new.gif"
    54.     images[42]="/styles/lightskin/attach/jpg.gif"
    55.     images[43]="/styles/lightskin/attach/zip.gif"
    56.     images[44]="/styles/lightskin/attach/jpeg.gif"
    57.     images[45]="/styles/lightskin/attach/pdf.gif"
    58.     images[46]="/styles/lightskin/attach/jpe.gif"
    59.     images[47]="/styles/lightskin/attach/mp3.gif"
    60.     images[48]="/styles/lightskin/attach/wmv.gif"
    61.     images[49]="/styles/lightskin/attach/png.gif"
    62.     images[50]="/styles/lightskin/attach/attach.gif"
    63.     images[51]="/styles/lightskin/attach/txt.gif"
    64.     images[52]="/styles/lightskin/attach/rtf.gif"
    65.     images[53]="/styles/lightskin/attach/php.gif"
    66.     images[54]="/styles/lightskin/attach/tiff.gif"
    67.     images[55]="/styles/lightskin/attach/bmp.gif"
    68.     images[56]="/styles/lightskin/attach/psd.gif"
    69.     images[57]="/styles/lightskin/attach/tif.gif"
    70.     images[58]="/styles/lightskin/attach/xml.gif"
    71.     images[59]="/styles/lightskin/attach/doc.gif"
    72.     images[60]="/styles/lightskin/attach/gif.gif"
    73.     images[61]="/styles/lightskin/rating/rating_4.gif"
    74.     images[62]="/styles/lightskin/rating/rating_3.gif"
    75.     images[63]="/styles/lightskin/rating/rating_5.gif"
    76.     images[64]="/styles/lightskin/rating/rating_1.gif"
    77.     images[65]="/styles/lightskin/rating/rating_0.gif"
    78.     images[66]="/styles/lightskin/rating/rating_2.gif"
    79.     images[67]="/styles/lightskin/editor/copy.gif"
    80.     images[68]="/styles/lightskin/editor/paste.gif"
    81.     images[69]="/styles/lightskin/editor/insertorderedlist.gif"
    82.     images[70]="/styles/lightskin/editor/link.gif"
    83.     images[71]="/styles/lightskin/editor/resize_0.gif"
    84.     images[72]="/styles/lightskin/editor/resize_1.gif"
    85.     images[73]="/styles/lightskin/editor/justifyleft.gif"
    86.     images[74]="/styles/lightskin/editor/insertimage.gif"
    87.     images[75]="/styles/lightskin/editor/unlink.gif"
    88.     images[76]="/styles/lightskin/editor/attach.gif"
    89.     images[77]="/styles/lightskin/editor/spelling.gif"
    90.     images[78]="/styles/lightskin/editor/outdent.gif"
    91.     images[79]="/styles/lightskin/editor/justifycenter.gif"
    92.     images[80]="/styles/lightskin/editor/redo.gif"
    93.     images[81]="/styles/lightskin/editor/separator.gif"
    94.     images[82]="/styles/lightskin/editor/createlink.gif"
    95.     images[83]="/styles/lightskin/editor/bold.gif"
    96.     images[84]="/styles/lightskin/editor/html.gif"
    97.     images[85]="/styles/lightskin/editor/email.gif"
    98.     images[86]="/styles/lightskin/editor/quote.gif"
    99.     images[87]="/styles/lightskin/editor/underline.gif"
    100.     images[88]="/styles/lightskin/editor/insertunorderedlist.gif"
    101.     images[89]="/styles/lightskin/editor/undo.gif"
    102.     images[90]="/styles/lightskin/editor/justifyright.gif"
    103.     images[91]="/styles/lightskin/editor/indent.gif"
    104.     images[92]="/styles/lightskin/editor/italic.gif"
    105.     images[93]="/styles/lightskin/editor/paperclip.gif"
    106.     images[94]="/styles/lightskin/editor/removeformat.gif"
    107.     images[95]="/styles/lightskin/editor/menupop.gif"
    108.     images[96]="/styles/lightskin/editor/php.gif"
    109.     images[97]="/styles/lightskin/editor/color.gif"
    110.     images[98]="/styles/lightskin/editor/code.gif"
    111.     images[99]="/styles/lightskin/editor/cut.gif"
    112.     images[100]="/styles/lightskin/editor/smilie.gif"
    113.     images[101]="/styles/lightskin/editor/switchmode.gif"
    114.     images[102]="/styles/lightskin/buttons/collapse_tcat.gif"
    115.     images[103]="/styles/lightskin/buttons/yellowcard.gif"
    116.     images[104]="/styles/lightskin/buttons/mode_linear.gif"
    117.     images[105]="/styles/lightskin/buttons/redcard.gif"
    118.     images[106]="/styles/lightskin/buttons/reputation.gif"
    119.     images[107]="/styles/lightskin/buttons/post_thanks.gif"
    120.     images[108]="/styles/lightskin/buttons/find.gif"
    121.     images[109]="/styles/lightskin/buttons/upload_pictures.png"
    122.     images[110]="/styles/lightskin/buttons/firstnew.gif"
    123.     images[111]="/styles/lightskin/buttons/collapse_thead.gif"
    124.     images[112]="/styles/lightskin/buttons/sortasc.gif"
    125.     images[113]="/styles/lightskin/buttons/newthread.gif"
    126.     images[114]="/styles/lightskin/buttons/mode_hybrid.gif"
    127.     images[115]="/styles/lightskin/buttons/sendtofriend.gif"
    128.     images[116]="/styles/lightskin/buttons/printer.gif"
    129.     images[117]="/styles/lightskin/buttons/collapse_generic.gif"
    130.     images[118]="/styles/lightskin/buttons/reply_small.gif"
    131.     images[119]="/styles/lightskin/buttons/home.gif"
    132.     images[120]="/styles/lightskin/buttons/addpoll.gif"
    133.     images[121]="/styles/lightskin/buttons/forward.gif"
    134.     images[122]="/styles/lightskin/buttons/multiquote_off.gif"
    135.     images[123]="/styles/lightskin/buttons/multiquote_on.gif"
    136.     images[124]="/styles/lightskin/buttons/sortdesc.gif"
    137.     images[125]="/styles/lightskin/buttons/reply.gif"
    138.     images[126]="/styles/lightskin/buttons/sendpm.gif"
    139.     images[127]="/styles/lightskin/buttons/collapse_alt_collapsed.gif"
    140.     images[128]="/styles/lightskin/buttons/collapse_alt.gif"
    141.     images[129]="/styles/lightskin/buttons/lastpost.gif"
    142.     images[130]="/styles/lightskin/buttons/quickreply.gif"
    143.     images[131]="/styles/lightskin/buttons/report.gif"
    144.     images[132]="/styles/lightskin/buttons/threadclosed.gif"
    145.     images[133]="/styles/lightskin/buttons/mode_normal.gif"
    146.     images[134]="/styles/lightskin/buttons/ip.gif"
    147.     images[135]="/styles/lightskin/buttons/add_album.png"
    148.     images[136]="/styles/lightskin/buttons/email.gif"
    149.     images[137]="/styles/lightskin/buttons/mode_threaded.gif"
    150.     images[138]="/styles/lightskin/buttons/quote.gif"
    151.     images[139]="/styles/lightskin/buttons/edit.gif"
    152.     images[140]="/styles/lightskin/buttons/viewpost.gif"
    153.     images[141]="/styles/lightskin/buttons/collapse_generic_collapsed.gif"
    154.     images[142]="/styles/lightskin/buttons/infraction.gif"
    155.     images[143]="/styles/lightskin/buttons/subscribe.gif"
    156.     images[144]="/styles/lightskin/buttons/collapse_thead_collapsed.gif"
    157.     images[145]="/styles/lightskin/buttons/collapse_tcat_collapsed.gif"
    158.     images[146]="/styles/lightskin/reputation/reputation_balance.gif"
    159.     images[147]="/styles/lightskin/reputation/reputation_pos.gif"
    160.     images[148]="/styles/lightskin/reputation/reputation_off.gif"
    161.     images[149]="/styles/lightskin/reputation/reputation_neg.gif"
    162.     images[150]="/styles/lightskin/reputation/reputation_highneg.gif"
    163.     images[151]="/styles/lightskin/reputation/reputation_highpos.gif"
    164.     images[152]="/styles/lightskin/misc/question_icon.gif"
    165.     images[153]="/styles/lightskin/misc/13x13arrowdown.gif"
    166.     images[154]="/styles/lightskin/misc/skype_message.gif"
    167.     images[155]="/styles/lightskin/misc/birthday_small.gif"
    168.     images[156]="/styles/lightskin/misc/skype_voicemail.gif"
    169.     images[157]="/styles/lightskin/misc/album_folder_bg.png"
    170.     images[158]="/styles/lightskin/misc/bookmarksite_google.gif"
    171.     images[159]="/styles/lightskin/misc/calendar_popup.png"
    172.     images[160]="/styles/lightskin/misc/vbulletin2_logo.gif"
    173.     images[161]="/styles/lightskin/misc/trashcan_small.gif"
    174.     images[162]="/styles/lightskin/misc/down9x9.gif"
    175.     images[163]="/styles/lightskin/misc/unknown.gif"
    176.     images[164]="/styles/lightskin/misc/poll_posticon.gif"
    177.     images[165]="/styles/lightskin/misc/whos_online.gif"
    178.     images[166]="/styles/lightskin/misc/comment_add.png"
    179.     images[167]="/styles/lightskin/misc/calendar.gif"
    180.     images[168]="/styles/lightskin/misc/tree_ltr.gif"
    181.     images[169]="/styles/lightskin/misc/v.gif"
    182.     images[170]="/styles/lightskin/misc/birthday.gif"
    183.     images[171]="/styles/lightskin/misc/cross.png"
    184.     images[172]="/styles/lightskin/misc/rss.jpg"
    185.     images[173]="/styles/lightskin/misc/vbulletin3_logo_white.gif"
    186.     images[174]="/styles/lightskin/misc/calendar_icon.gif"
    187.     images[175]="/styles/lightskin/misc/menu_open.gif"
    188.     images[176]="/styles/lightskin/misc/skype_addcontact.gif"
    189.     images[177]="/styles/lightskin/misc/expires.gif"
    190.     images[178]="/styles/lightskin/misc/skype_info.gif"
    191.     images[179]="/styles/lightskin/misc/im_icq.gif"
    192.     images[180]="/styles/lightskin/misc/menu_background.gif"
    193.     images[181]="/styles/lightskin/misc/moderated_small.gif"
    194.     images[182]="/styles/lightskin/misc/im_aim.gif"
    195.     images[183]="/styles/lightskin/misc/navbits_finallink_ltr.gif"
    196.     images[184]="/styles/lightskin/misc/navbits_finallink.gif"
    197.     images[185]="/styles/lightskin/misc/skype_callstart.gif"
    198.     images[186]="/styles/lightskin/misc/tag.png"
    199.     images[187]="/styles/lightskin/misc/moderated.gif"
    200.     images[188]="/styles/lightskin/misc/skype_fileupload.gif"
    201.     images[189]="/styles/lightskin/misc/multipage.gif"
    202.     images[190]="/styles/lightskin/misc/colorpicker_close.gif"
    203.     images[191]="/styles/lightskin/misc/tree_tr.gif"
    204.     images[192]="/styles/lightskin/misc/tree_rtl.gif"
    205.     images[193]="/styles/lightskin/misc/tick.png"
    206.     images[194]="/styles/lightskin/misc/tree_ir.gif"
    207.     images[195]="/styles/lightskin/misc/album_folder_tab.png"
    208.     images[196]="/styles/lightskin/misc/navbits_finallink_rtl.gif"
    209.     images[197]="/styles/lightskin/misc/tree_t.gif"
    210.     images[198]="/styles/lightskin/misc/bookmarksite_stumbleupon.gif"
    211.     images[199]="/styles/lightskin/misc/im_msn.gif"
    212.     images[200]="/styles/lightskin/misc/album_folder_top_bg.gif"
    213.     images[201]="/styles/lightskin/misc/spam_detected.png"
    214.     images[202]="/styles/lightskin/misc/subscribed.gif"
    215.     images[203]="/styles/lightskin/misc/13x13progress.gif"
    216.     images[204]="/styles/lightskin/misc/colorpicker_transparent.gif"
    217.     images[205]="/styles/lightskin/misc/vbulletin3_logo_grey.gif"
    218.     images[206]="/styles/lightskin/misc/navbits_start.gif"
    219.     images[207]="/styles/lightskin/misc/subscribed_event.gif"
    220.     images[208]="/styles/lightskin/misc/paperclip.gif"
    221.     images[209]="/styles/lightskin/misc/magnifier.png"
    222.     images[210]="/styles/lightskin/misc/sticky.gif"
    223.     images[211]="/styles/lightskin/misc/bookmarksite_digg.gif"
    224.     images[212]="/styles/lightskin/misc/progress.gif"
    225.     images[213]="/styles/lightskin/misc/menu_open_usercss.gif"
    226.     images[214]="/styles/lightskin/misc/redcard_small.gif"
    227.     images[215]="/styles/lightskin/misc/im_skype.gif"
    228.     images[216]="/styles/lightskin/misc/userfield_edit.gif"
    229.     images[217]="/styles/lightskin/misc/trashcan.gif"
    230.     images[218]="/styles/lightskin/misc/yellowcard_small.gif"
    231.     images[219]="/styles/lightskin/misc/bookmarksite_delicious.gif"
    232.     images[220]="/styles/lightskin/misc/tree_i.gif"
    233.     images[221]="/styles/lightskin/misc/unknown_sg.gif"
    234.     images[222]="/styles/lightskin/misc/im_yahoo.gif"
    235.     images[223]="/styles/lightskin/misc/lightbox_progress.gif"
    236.     images[224]="/styles/lightskin/misc/11x11progress.gif"
    237.     images[225]="/styles/lightskin/misc/stats.gif"
    238.     images[226]="/styles/lightskin/polls/bar2.gif"
    239.     images[227]="/styles/lightskin/polls/bar5-l.gif"
    240.     images[228]="/styles/lightskin/polls/bar1-r.gif"
    241.     images[229]="/styles/lightskin/polls/bar4-r.gif"
    242.     images[230]="/styles/lightskin/polls/bar2-l.gif"
    243.     images[231]="/styles/lightskin/polls/bar1.gif"
    244.     images[232]="/styles/lightskin/polls/bar5.gif"
    245.     images[233]="/styles/lightskin/polls/bar2-r.gif"
    246.     images[234]="/styles/lightskin/polls/bar6-r.gif"
    247.     images[235]="/styles/lightskin/polls/bar3-r.gif"
    248.     images[236]="/styles/lightskin/polls/bar6.gif"
    249.     images[237]="/styles/lightskin/polls/bar4.gif"
    250.     images[238]="/styles/lightskin/polls/bar5-r.gif"
    251.     images[239]="/styles/lightskin/polls/bar4-l.gif"
    252.     images[240]="/styles/lightskin/polls/bar6-l.gif"
    253.     images[241]="/styles/lightskin/polls/bar1-l.gif"
    254.     images[242]="/styles/lightskin/polls/bar3.gif"
    255.     images[243]="/styles/lightskin/polls/bar3-l.gif"
    256.     images[244]="/styles/lightskin/layout/top_l.png"
    257.     images[245]="/styles/lightskin/layout/tcat_main.png"
    258.     images[246]="/styles/lightskin/layout/logout.png"
    259.     images[247]="/styles/lightskin/layout/commu.png"
    260.     images[248]="/styles/lightskin/layout/left_y.png"
    261.     images[249]="/styles/lightskin/layout/profile.png"
    262.     images[250]="/styles/lightskin/layout/faq.png"
    263.     images[251]="/styles/lightskin/layout/reg.png"
    264.     images[252]="/styles/lightskin/layout/alt2.png"
    265.     images[253]="/styles/lightskin/layout/beta.png"
    266.     images[254]="/styles/lightskin/layout/cal.png"
    267.     images[255]="/styles/lightskin/layout/pro_bot.png"
    268.     images[256]="/styles/lightskin/layout/logo5.png"
    269.     images[257]="/styles/lightskin/layout/pro.png"
    270.     images[258]="/styles/lightskin/layout/member.png"
    271.     images[259]="/styles/lightskin/layout/register.png"
    272.     images[260]="/styles/lightskin/layout/usercp.png"
    273.     images[261]="/styles/lightskin/layout/line.png"
    274.     images[262]="/styles/lightskin/layout/tcat.gif"
    275.     images[263]="/styles/lightskin/layout/logo.png"
    276.     images[264]="/styles/lightskin/layout/search_bg.png"
    277.     images[265]="/styles/lightskin/layout/noavatar.png"
    278.     images[266]="/styles/lightskin/layout/bot_x.png"
    279.     images[267]="/styles/lightskin/layout/quick.png"
    280.     images[268]="/styles/lightskin/layout/bot_r.png"
    281.     images[269]="/styles/lightskin/layout/bot_l.png"
    282.     images[270]="/styles/lightskin/layout/search_button.png"
    283.     images[271]="/styles/lightskin/layout/right_y.png"
    284.     images[272]="/styles/lightskin/layout/thead.gif"
    285.     images[273]="/styles/lightskin/layout/avatar.png"
    286.     images[274]="/styles/lightskin/layout/news.png"
    287.     images[275]="/styles/lightskin/layout/ads.png"
    288.     images[276]="/styles/lightskin/layout/foot.png"
    289.     images[277]="/styles/lightskin/layout/top_r.png"
    290.     images[278]="/styles/lightskin/layout/alt1.png"
    291.     images[279]="/styles/lightskin/layout/background.png"
    292.     images[280]="/styles/lightskin/layout/trans.png"
    293.     images[281]="/styles/lightskin/layout/top_x.png"
    294.     images[282]="/styles/lightskin/layout/newpost.png"
    295.     images[283]="/styles/lightskin/layout/pro_x.png"
    296.  
    297.     // start preloading
    298.      for(i=0; i<=3; i++)
    299.      {
    300.           imageObj.src=images[i];
    301.      }
    302.  
    303. }
    304. </script></head>
    305. <body onLoad="javascript:preloader()">
    306. </body>
    307. </html>
    308.  
     
    !Unreal, May 6, 2009 IP
  2. Aaron Sustar

    Aaron Sustar Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Whoa, that's A LOT of server requests you will be getting for each first-time visitor.

    But if you really need to preload all these images, why don't you simply add them at the end of your HTML code with a style="display:none;" or an appropriate CSS class? The browser will request them from the server anyways, even though they are not displayed when the page first loads.
     
    Aaron Sustar, May 7, 2009 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    erm - do you realise you need to actually WAIT for the image to load after the .src assignment before you pass on the next one? the speed with which a loop runs is 10000s of time faster than the possible download time of your image sets.

    you can run it in a closure/anon function and make a new image object for each one, that way they would thread the download and you can get some events going like, onprogress, oncomplete etc.
     
    dimitar christoff, May 7, 2009 IP
  4. fadetoblack22

    fadetoblack22 Active Member

    Messages:
    2,174
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Don't use display: none because this causes some browsers to not cache them. Instead call them to the css with position absoulute set to off screen and when you include the image file on the page, set the height and width small.
     
    fadetoblack22, May 7, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I haven't tested this but if you change the bottom part to:
    Code (Text):
    1.  
    2.     // start preloading
    3.     imageObj.src = images[i];
    4.  
    5.     imageObj.onComplete = function() {
    6.         if(i<images.length) {
    7.         i++;
    8.         imageObj.src=images[i];
    9.         }
    10.     }
    11. }
    12.  
     
    camjohnson95, May 11, 2009 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    i think the correct event here is onload and not oncomplete...

    however, if I remember correctly, some browsers don't implement this and there were some differences in the timing of the event as well...

    PHP:
    1. // evil, incosistent in opera and IE
    2. var image = new Image();
    3. image.src = 'image.jpg'; // instant if cached, hence line below won't run.
    4. image.onload = function() {
    5.     // sometimes called
    6.     alert('image loaded');
    7. };
    8.  
    9. // better:
    10. var image = new Image();
    11. image.onload = function() {
    12.     // always called
    13.     alert('image loaded');
    14. };
    15. image.src = 'image.jpg';
    16.  
    i have always done my image preloaders with mootools' Asset.images([img1, img2, .... imgn], options);
    so i thought, lets try this in vanilla javascript and... it works (shocking!)

    PHP:
    1. <html>
    2. <head>
    3. <script language="javascript">
    4.  
    5. // assign images as global
    6. var imagesArray = [
    7.     "8-0.gif",
    8.     "article-bg.gif",
    9.     "article-page-bg.gif",
    10.     "big-bg.gif",
    11.     "body-bg.gif",
    12.     "bottom-end.gif",
    13.     "buzz.gif",
    14.     "carousel.jpg",
    15.     "close.gif",
    16.     "comment-button.gif",
    17.     "crop.gif",
    18.     "delicious.gif",
    19.     "digg.gif",
    20.     "follow-me-twitter.gif",
    21.     "goback-button.gif",
    22.     "go.gif",
    23.     "goog.png",
    24.     "icon.gif",
    25.     "itembg.gif",
    26.     "li-bg.gif",
    27.     "mister-wong.gif",
    28.     "mixx.gif",
    29.     "moostack.jpg",
    30.     "point_down.gif",
    31.     "progressbar-orange.gif",
    32.     "run-batch.gif",
    33.     "search-bg.gif",
    34.     "se_bubble.gif",
    35.     "se_bubbles.gif",
    36.     "sidebar-bg.gif",
    37.     "sidebar-gap.gif",
    38.     "side-bg-top.gif",
    39.     "social_bookmark_bg.gif",
    40.     "star.gif",
    41.     "stereo.gif",
    42.     "stumble.gif",
    43.     "tags.gif",
    44.     "targetter.gif",
    45.     "textarea-bg.gif",
    46.     "tooltip-ie.gif",
    47.     "tooltip.png",
    48.     "top-bg.gif",
    49.     "top-body-bg.jpg"
    50. ];
    51.  
    52. window.onload = function() {
    53.     // local scope. strictly speaking, images loading does not need to wait for onload, so you can change this.
    54.  
    55.     // local variables and function assignments.
    56.     var loadedImages = [], reportProgress = function(where, what) {
    57.         // used to show how many images loaded thus far and / or trigger some event you can use when done.
    58.  
    59.         // optional to show onscreen...
    60.         var output = "loaded ";
    61.         output += loadedImages.length;
    62.         output += " of ";
    63.         output += imagesArray.length;
    64.         output += " images.";
    65.         where.innerHTML = output;
    66.  
    67.         // this bit will fire when all images done:
    68.         if (imagesArray.length == loadedImages.length) {
    69.             // total images onComplete. done. rewrite as you deem fit.
    70.  
    71.             // keep in mind that if 1 of your images is a 404, this function won't trigger.
    72.             // alert("done");
    73.  
    74.             // you can loop through them (if you use the objects, then output loadedImages[x].src or inject them
    75.             for (x in loadedImages) {
    76.                 where.innerHTML +=  "<br />" + loadedImages[x];
    77.             }
    78.         }
    79.     }, loadImage = function(imageSrc, reportDiv) {
    80.         // actual function that loads image into DOM
    81.  
    82.         var image = new Image(); // local scope, new object created per instance.
    83.  
    84.         image.onload = function() {
    85.             if (!image) return;
    86.            
    87.             loadedImages.push(image.src); // record this image path as loaded in global scope
    88.             // or... store the objects themselves so you can inject them:
    89.             // this.onload = null;
    90.             // loadedImages.push(this);
    91.  
    92.             // sample report of progress
    93.             reportProgress(reportDiv, image.src);
    94.  
    95.             // remove event and self to prevent IE from animated gif loops(see bottom) and clear up garbage objects.
    96.             image = image.onload = image.onabort = image.onerror = null;
    97.         };
    98.  
    99.         image.src = imageSrc;
    100.     }, total = imagesArray.length, loadedProgress = document.getElementById("mydiv"); // local scope after load.
    101.  
    102.     // loop through the images and load.
    103.     while(total-- || loadedImages.length == imagesArray.length) {
    104.         loadImage("http://fragged.org/images/" + imagesArray[total], loadedProgress, total);
    105.     }
    106. };
    107. </script>
    108. </head>
    109. <body>
    110.  
    111. <div id="mydiv">Loading images...</div>
    112. </body>
    113. </html>
    114.  
    115.  
    this script will load whatever images i had in my images folder on http://fragged.org/ at the time of writing. it works in firefox, ie7, opera 9.6 and safari4 beta (that I tested thus far).

    and yes, you can replace the window.onload with an anon closure instead... we don't need to wait for page onload to start loading the images but i am anal like that :D

    *edit* i have now worked out that IE fires an onload event for animated gif loops. meaning, each time the animated gif run its sequence, the onload function run again, causing an endless set of reports and messing up the script. I have edited it to remove the event - but to be safe, I am setting the object and all possible events to null, collecting garbage as best as we can... careful if you pass on the object and not the path to the image to the loadedImages array, make sure this.onload = null beforehand.
     
    dimitar christoff, May 11, 2009 IP
    FilmFiddler likes this.
  7. seokingdom07

    seokingdom07 Banned

    Messages:
    132
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yes mototools is good option to preload the image. I oftenly use mototools for preloading images if needed
     
    seokingdom07, May 17, 2009 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8
    dimitar christoff, May 18, 2009 IP