Wondering if anyone can help because no one out there has given a solution this problem!
How do you wait for a dynamically loaded element (e.g. a specific div of another .html page) to first be fully loaded so that you can then, and only then, trigger a function?
.load
obviously won't work as pointed out by the OP of this
post.
Any ideas?
Many thanks!
Update:
Some more info: I am using .load
to load a div element from another page which contains data for use by Galleria (executed using .load
's callback). I am currently using setTimeout
to ensure the div is loaded so that Galleria doesn't load too quickly and empty. But I don't like this setTimeout
business, it's too much of a patchwork solution.
Divs to be loaded:
<div id="source1" class="container">
<div class="thumbnails"><a href="images/gallery/recent/7.jpg"><img src="images/navigation/recent/7.jpg"/></a></div>
<div class="thumbnails"><a href="images/gallery/recent/6.jpg"><img src="images/navigation/recent/6.jpg"/></a></div>
<div class="thumbnails"><a href="images/gallery/recent/5.jpg"><img src="images/navigation/recent/5.jpg"/></a></div>
<div class="thumbnails"><a href="images/gallery/recent/4.jpg"><img src="images/navigation/recent/4.jpg"/></a></div>
</div>
<div id="source2" class="container">
<div class="thumbnails"><a href="images/gallery/recent/3.jpg"><img src="images/navigation/recent/3.jpg"/></a></div>
<div class="thumbnails"><a href="images/gallery/recent/2.jpg"><img src="images/navigation/recent/2.jpg"/></a></div>
<div class="thumbnails"><a href="images/gallery/recent/1.jpg"><img src="images/navigation/recent/1.jpg"/></a></div>
</div>
Part of the Script:
$(document).on("click", "#menu", function(event) {
event.preventDefault();
var link = this.hash;
var ref = this.hash.replace("#", "") + ".html " + link;
var deferred2 = $.Deferred();
goLoad();
function goLoad(){
switch(link){
case "#recent":
$("#recent").css("height", window.innerHeight);
$("#recent").load(ref + " > *", null, goRecent());
break;
/*..other cases...*/
}
};
function goRecent(){
setTimeout(function(){
deferred2.resolve();
deferred2.done(doGallerySize, doGallery, goScroll)
function doGallerySize(){
$("#gallery").css("height", window.innerHeight);
}
function doGallery(){
Galleria.run("#galleria",{
dataSource:"#source1, #source2",
keepSource:true,
responsive:true,
thumbnails:false
});
}
function goScroll(){
$.smoothScroll({
scrollTarget: link,
afterScroll: function() {
$(".stickynav").sticky({topSpacing:0});}
});
}
}, 100);
}
});