Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign event to div when it has loaded fully

Can I assign an event to a div to fire when all elements of the containing div have loaded fully? eg. if the div contains an image, fire an event on the div when the image has loaded fully. I am using jquery.

like image 549
amateur Avatar asked Nov 12 '10 00:11

amateur


People also ask

How do you add onload event to a div element?

The onload event can only be used on the document(body) itself, frames, images, and scripts. In other words, it can be attached to only body and/or each external resource. The div is not an external resource and it's loaded as part of the body, so the onload event doesn't apply there.

Which event would you use to start a script after a page has been fully loaded by the browser?

The onload event occurs when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

Which event triggers when object is loaded?

onload. The load event on the window object triggers when the whole page is loaded including styles, images and other resources. This event is available via the onload property.

What is load event?

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded , which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading. This event is not cancelable and does not bubble.


Video Answer


2 Answers

Not sure how you're dynamically adding your content, but this example should illustrate how it could work.

It is using .append() to simulate your dynamically loaded content. Then after the append, it uses .find() to find the images, and .load() to attach a load handler to them.

Finally, it returns the length property of the images. Then in the load handler, as the images finish loading, the length is decremented. Once it gets to 0, all the images are loaded, and the code inside the if() runs.

Example: http://jsfiddle.net/ehwyF/

var len = $('#mydiv').append('<img src = "http://dummyimage.com/150x90/f00/fff.png&text=my+image" /><img src = "http://dummyimage.com/160x90/f00/fff.png&text=my+image" /><img src = "http://dummyimage.com/170x90/f00/fff.png&text=my+image" /><img src = "http://dummyimage.com/180x90/f00/fff.png&text=my+image" />')
    .find('img')
    .load(function() {
        if( --len === 0) {
            alert('all are loaded');
        }
    }).length;

This way, the code runs based only on the images in #mydiv.

If there are any images in there that were not dynamic, and therefore shouldn't get the .load() event, you should be able to add a .not() filter to exclude ones the complete property is true.

Place this after the .find() and before the .load().

.not(function(){return this.complete})
like image 159
user113716 Avatar answered Sep 24 '22 21:09

user113716


If it's just one image you're wanting to wait for you can load the image with ajax and then trigger the event in the ajax callback.

like image 40
generalhenry Avatar answered Sep 24 '22 21:09

generalhenry