/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Waiting on div contents

I’m looking for something like the following

[CODE]$(‘#divName’).contents().ready(function(){
alert(‘ready’);
});[/CODE]

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

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscNov 18.2010 — .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):

<i>
</i>&lt;div id="mydiv"&gt;&lt;img src="very/large/image.jpg"/&gt;&lt;/div&gt;


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:

<i>
</i>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();
$(img_).load(checkLoaded).attr('src',myImages[thisImage]);
}

function checkLoaded() {
if(imagesLoaded != myImages.length) { imagesLoaded++; return; }

/* your code here will be run once all images are loaded */
}



&lt;div id="mydiv"&gt;
&lt;img src="very/large/image1.jpg"/&gt;
&lt;img src="very/large/image2.jpg"/&gt;
&lt;img src="very/large/image3.jpg"/&gt;
&lt;img src="very/large/image4.jpg"/&gt;
&lt;/div&gt;
Copy linkTweet thisAlerts:
@sNagheenanajarauthorNov 23.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?
Copy linkTweet thisAlerts:
@aj_nscNov 24.2010 — 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.
Copy linkTweet thisAlerts:
@sohguanhNov 24.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 ?

E.g

<body onload="XXXX">

...

</body>
Copy linkTweet thisAlerts:
@aj_nscNov 24.2010 — 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?
Copy linkTweet thisAlerts:
@sNagheenanajarauthorNov 29.2010 — So the html is inside a google maps InfoWindow, which is shown on a Markers click event
[CODE]...
_infoWindow = transformerInfoWindow;
_infoDiv = $('#transformerInfoDiv');
...
google.maps.event.addListener(marker, 'click', function(){
_infoDiv.html(getLocationEquipmentHTML(marker.functionalLocation));
_infoWindow.open(map, marker);
_infoDiv.show()
});
...[/CODE]


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.

[CODE]function getLocationEquipmentHTML(_location){
equipmentHTML = '';
$.getJSON('getLocationEquipment', {
location: _location
}, function(json){
$(json).each(function(){
equipmentHTML = equipmentHTML + '<a id="' + $(this)[0] + 'Link" href="javascript:alert('+$(this)[0]+');" >' + $(this)[0] + '</a><br/>'
});
});
//alert('debug spot');
return equipmentHTML;
}[/CODE]


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.
Copy linkTweet thisAlerts:
@sNagheenanajarauthorNov 30.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.
Copy linkTweet thisAlerts:
@sNagheenanajarauthorDec 04.2010 — This line doesn't appear to work in chrome or firefox
[CODE]$(img_).load(checkLoaded).attr('src', json[i]);[/CODE]

any thoughts?
×

Success!

Help @sNagheenanajar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...