There are a lot of things that seem very strange about what you're doing.
Basic preloading can be a simple as:

function loadImages() {
        $.each(arguments, function(i, url) {
                var image = new Image();
                image.onload = function() {
                        // do something with the image now that it's loaded
                };
                image.src = url;
        });
};

$(window).load(function() {
        loadImages('foo.jpg', 'bar.jpg', 'far.jpg');
});

If you want something a little more multi-purpose, you could do something
like:

function loadImages(urls, onLoad, onFinished) {
        var loaded = 0;
        urls = (url.constructor == Array) ? urls : [urls];
        $.each(urls, function(i, url) {
                var image = new Image();
                image.onload = function() {
                        loaded++;
                        if($.isFunction(onLoad)) {
                                onLoad.call(this);
                        }
                        if(loadedCount == urls.length &&
$.isFunction(onFinished)) {
                                onFinished.call(this)
                        }
                };
                image.src = url;
        });
};

$(window).load(function() {
        loadImages(['foo.jpg', 'bar.jpg', 'far.jpg'], function() {
                $('#images').append(this);
        }, function() {
                $('#images').show();
        });
});

It kind of depends on your application. But like I said, you're doing a lot
of strange things in your current code. Maybe you should describe how
exactly you want to it work.

--Erik


On 9/25/07, dimitrisX < [EMAIL PROTECTED]> wrote:
>
>
> Hello,
> I am trying to preload some images using either one of the functions
> below. They don't seem to load. I really could use your help here.
>
> /// version 1
> jQuery.preloadImages = function()
> {
>         for(var i = 0; i<arguments.length; i++)
>         {
>             $('<img>').attr('src', arguments[i]);
>         }
>
> }
>
> /// version 2 (not quite like the original)
> jQuery.preloadImages2 = function(){
>         var args = arguments;
>         $(window).bind('load', function(){
>            var preload = new Array();
>            for(var i = 0; i<args.length; i++){
>                         preload[i] = args[i];
>                 }
>            $(document.createElement('img')).bind('load', function(){
>                         if(preload[0]){
>                                 this.src = preload.shift();
>                                 alert(this.src);
>                         }
>            }).trigger('load').appendTo('#imagePanel');
>         });
>
> }
>
> I then call one of those functions before the document loads, passing
> the desired image pathnames as arguments:
>
> $.preloadImages(
>         "img/cedefop.jpg",
>         "img/cedefop5.jpg",
>         "img/sap.jpg",
>         "img/igme5.jpg",
>         "img/naxm.jpg",
>         "img/olymp.jpg"
> );
>
> ...and then i try to append the preloaded images to a div element:
>
> $(document).ready( function(){
>         images = $('img');
>         $('#imagePanel').append(images)
>
> });
>
> The images will not load. What is wrong here?
> Thank you,
>
>

Reply via email to