I'm currently in the middle of trying to figure out how I can load / preload a large number of content on to a long page website.
The solution I thought of, is to load content, after a previous part has loaded.
THe HTML setup looks like this:
<div class="panel">Panel 1</div>
<div class="panel">Panel 2</div>
<div class="panel">Panel 3</div>
<div class="panel">Panel 4</div>
Is there a way to load Panel 1, then once that has loaded, load Panel 2 and so on...
With most solutions I've looked at, they have suggested loading the content through an external file using the .load() method in jQuery.
If anybody has any advice and/or examples or example code that would be AMAZING.
You can go as well with
each
. Assuming your setup being final:or around that thought, but you must have a way to distinct between the panels to load the correct one. This solution should be good for any number of panels.
If you really want to load it separately, that's exactly what jQuery's
load
is for. For example, suppose you put adata-page
attribute on those elements telling each of them what content it should have:(You don't have to do that, it's just one way you might tell them.)
Then loading them sequentially (if you really want it to be sequential) is fairly straight-forward:
That works by triggering the first panel's load when the page is first rendered, and then triggering each of the subsequent panel loads using the completion handler of the previous
load
call.Live Example | Source
In the comments below you talked about waiting for images and such to complete as well. Lacking a
body
element for the masteronload
that you get with windows, we have to check for incomplete images (HTMLImageElement
has acomplete
property) and hook their load event. We load the next panel when there are no more incomplete images:Live Example | Source
That only handles images, but you should be able to generalize the concept for videos as well (I haven't dealt with
video
tags, and I don't know that you're using them as opposed to Flash videos, etc. — which I also haven't dealt with).