2

I'm trying to preload images with javascript:

$(document).ready(function () {
    preloadImages(
        ['../../Content/Resources/close_mouse_over.png',
        '../../Content/Resources/close.png']);
});

function preloadImages(sources) {
        var image = new Array();
        for (var i = 0; i < sources.length; i++) {
            image[i] = new Image();
            image[i].src = sources[i];
        }
    }

function mouseOverForImage(imgId, imgSrcs) {
        document.getElementById(imgId).src = imgSrcs;
    }

In Html:

<input type="image" src="../../Content/Resources/close.png" name="Action" value="Save" onmouseover="mouseOverForImage('close', '../../Content/Resources/close_mouse_over.png')" 
               onmouseout = "mouseOverForImage('close', '../../Content/Resources/close.png')" id="close" title = "Close" /> 

But request is still sending to server after mouseover. Is not working only in chrome

5
  • IIRC, preloading images doesn't work if it's too early, i.e. document.images is still undefined. Commented May 21, 2012 at 9:16
  • Try replacing image[i].src = sources[i]; with image[i].src = sources[i][0];. Commented May 21, 2012 at 9:17
  • Which browser are you using? Also you probably want to use a single array: preloadImages(['../../Content/Resources/close_mouse_over.png', '../../Content/Resources/close.png' ]); Commented May 21, 2012 at 9:22
  • I'm using chrome and i changed to single array, but it still does not work :( Commented May 21, 2012 at 9:39
  • @DarinDimitrov Maybe preloading works, but image caching is disabled. How do you think? Commented May 21, 2012 at 10:33

1 Answer 1

1

As noted within my comment, you are passing an array of arrays to the preLoadImages method.

As a result, you are passing an array to image[i].src which means it won't get loaded.

Try

$(document).ready(function () {
    preloadImages(
        ['../../Content/Resources/close_mouse_over.png', '../../Content/Resources/close.png']);
});

function preloadImages(sources) {
        var image = new Array();
        for (var i = 0; i < sources.length; i++) {
            image[i] = new Image();
            image[i].src = sources[i];
        }
    }

function mouseOverForImage(imgId, imgSrcs) {
        document.getElementById(imgId).src = imgSrcs;
    }

or, if you want to keep the array of array's, then use

function preloadImages(sources) {
        var image = new Array();
        for (var i = 0; i < sources.length; i++) {
            image[i] = new Image();
            image[i].src = sources[i][0];
        }
    }

Edit, on further investigation, a possible cause is preloadImages destroys the image array after preloading the images.

try this instead:

function preloadImages(sources) {
    window.preloadedImages = new Array();
    for (var i = 0; i < sources.length; i++) {
        window.preloadedImages[i] = new Image();
        window.preloadedImages[i].src = sources[i];
    }
}

This stores the preloaded images within the window object, allowing them to preload properly.

Hope that helps?

Sign up to request clarification or add additional context in comments.

5 Comments

I don't understand why ['../../Content/Resources/close_mouse_over.png', '../../Content/Resources/close.png'] is array of arrays, it's simple array
You are right about array of arrays, but it still does not work
Do you happen to have a link to the page that isn't working correctly? How are you testing to see if it is working or not?
Also, what may be happening, is that you are creating an array within preloadImages which is getting destroyed once you finish preloading the images. See my answer for an explanation.
I'm placing breakpoint in Application_PreRequestHandlerExecute event and it fires if request is received

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.