Results 1 to 9 of 9

Thread: [RESOLVED] Waiting on div contents

  1. #1
    Join Date
    Nov 2010

    resolved [RESOLVED] Waiting on div contents

    I'm looking for something like the following

    Basically I have a div with images. I need to know what event I can monitor for when all the pictures have downloaded.

  2. #2
    Join Date
    Dec 2005
    .ready() won't work anyway. It doesn't tell you when the images have loaded, it fires when the DOM has been rendered properly, which takes place long before the images are downloaded.

    The only way to fire an event after an image has downloaded, that I know of, is to load it up through Javascript itself.

    The following would probably work, though (not tested):

    <div id="mydiv"><img src="very/large/image.jpg"/></div>
    var img_ = new Image();
    $(img_).load(function() { /* image has downloaded */ }).attr('src','very/large/image.jpg');
    It doesn't have to be in that order, the Javascript part could be anywhere in the document. The reason why this should work is that as long as the url in the javascript and the img tag is the same, the browser will be requesting the same resource so the URL should be cached and both the image in the dom and the img specified in the Javascript function should be finished loading at the exact same time.

    If this does work, then just put your code where I have /* image has downloaded */ and it will work.

    This is an example for a single image, you'll have to be a little bit more inventive for multiple images.

    Like this:

    var imagesLoaded = 0;
    var myImages = ['very/large/image1.jpg','very/large/image2.jpg','very/large/image3.jpg','very/large/image4.jpg'];
    for(thisImage in myImages) {
      var img_ = new Image();
    function checkLoaded() {
      if(imagesLoaded != myImages.length) { imagesLoaded++; return; }
      /* your code here will be run once all images are loaded */
    <div id="mydiv">
     <img src="very/large/image1.jpg"/>
     <img src="very/large/image2.jpg"/>
     <img src="very/large/image3.jpg"/>
     <img src="very/large/image4.jpg"/>
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...

  3. #3
    Join Date
    Nov 2010
    this did the trick. now im having the same problem with a div of dynamically created hyperlinks. the div is hidden then populated and displayed on a click event. the first time the div is displayed it shows up blank, on the second mouse click it shows up with the list of hyperlinks just fine. Ive tried adapting this idea of the images to links but it is not working. thoughts?

  4. #4
    Join Date
    Dec 2005
    Images are different from other HTML content in that when you insert an img tag, it doesn't mean that content is loaded, but when you insert something like <a href="#">Link</a>, the content shows up with (virtually) no delay.

    Post your code.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...

  5. #5
    Join Date
    Mar 2010
    Then how about the onload event of the body tag. If we put Javascript code in the body tag onload event, we are guaranteed all images <img> tags including the DOM is completed already ?

    <body onload="XXXX">

  6. #6
    Join Date
    Dec 2005
    No. The only way (again that I know of) to tell if an image has downloaded it is to use a function like jQuery's load function followed by a callback function as I showed you the first time. All other onload, document.ready(), etc type events check to see if the DOM has been rendered properly.

    What is the code you are using to populate the DIV with links and then show/hide it onclick?
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...

  7. #7
    Join Date
    Nov 2010
    So the html is inside a google maps InfoWindow, which is shown on a Markers click event
    _infoWindow = transformerInfoWindow;
    _infoDiv = $('#transformerInfoDiv');
    google.maps.event.addListener(marker, 'click', function(){
        _infoWindow.open(map, marker);
    and here's my function that generates the html which is just a series of links for now, it will be a more complex form later on.

    function getLocationEquipmentHTML(_location){
        equipmentHTML = '';
        $.getJSON('getLocationEquipment', {
            location: _location
        }, function(json){
                equipmentHTML = equipmentHTML + '<a id="' + $(this)[0] + 'Link" href="javascript:alert('+$(this)[0]+');" >' + $(this)[0] + '</a><br/>'
        //alert('debug spot');
        return equipmentHTML;
    The InfoWindow's content always show up correctly on the second click. Sometimes the first click just produces a blank InfoWindow, sometimes it works on the first click. Sometimes if I click one marker, then click a marker I haven't clicked; the first markers html shows up in the second markers InfoWindow. Then of course if I click the second window again the correct html shows up.

    Also, when I include the alert the InfoWindow's content always shows up correctly.

  8. #8
    Join Date
    Nov 2010
    One more note, on the first click an AJAX call is being made. On the second click I'm not seeing any activity on my server so I assume the data is coming from the cache or something similar.

  9. #9
    Join Date
    Nov 2010

    Doesn't work in chrome or firefox

    This line doesn't appear to work in chrome or firefox
    $(img_).load(checkLoaded).attr('src', json[i]);
    any thoughts?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12807 seconds
  • Memory Usage 2,925KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates