How would I (or should I) extend Modernizr.load() to preload images?
Asked Answered
P

1

6

The idea is that the page /something/index.html is loaded. But, before I show it, its dependencies (css and images in this case) are preloaded.

Modernizr.load({
    load: ['/something/styles.css', '/something/image1.jpg'],
    complete: showFile
 });

I've looked into Paul Irish's "imagesLoaded" jQuery plug in, but I prefer the simplicity of using the loader I already have. I know YepNope (and Modernizr.load) aren't designer as generic preloaders, but I feel like this is the cleanest way to do this.

Would appreciate any thoughts on how to implement image preloading into a Modernizr/YepNope load script.

nz

Provoke answered 26/8, 2011 at 18:21 Comment(0)
D
6

On the yepnope documentation, it is specified that the preload! prefix "should" work on some other mime types.

You can try

Modernizr.load('preload!something/image1.jpg');

This work for me.

Don't forget to add the prefix plugin (else you will get an error when the js engine tries to execute the image) :

yepnope.addPrefix( 'preload', function ( resource ) {
    resource.noexec = true;
    return resource;
});
Disinclined answered 3/10, 2011 at 14:34 Comment(1)
I tried the preload call a dozen times and never once thought that I needed to extend yepnope with the plugin (I just assumed it was part of the script). Thanks for proving my reading comprehension isn't where it should be!Provoke

© 2022 - 2024 — McMap. All rights reserved.