Eerr help please, hopefully a quick one, unexpected object

Hi All,

I’ve got a jascript issue in the dreaded IE7/8 issue is unexpected object!

please see below script, any help would be fantastic, thank you!

<script type=“text/javascript”>
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$(‘<img/>’)[0].src = this;
});
}
preload([
‘images/slide1.jpg’,
‘images/slide2.jpg’,
‘images/slide3.jpg’,
‘images/slide4.jpg’,
‘images/slide0.jpg’
]);
</script>

Hi,

I’ve just tried this code in IE7 and it seemed to work fine. What version of jQuery are you using? I’m not sure if it makes any difference, but I used 1.10.1 in my test.

That’s an error message that you can expect to see if jQuery hasn’t been properly loaded.

I can’t reproduce this off the bat - do you have an example page where this is occurring?

If I may suggest an enhancement to the preload() function - it could be rewritten a little to be a little more performant.


function preload(arrayOfImages) {
    var i, $img = $("<img />");
        
    for (i = 0; i < arrayOfImages.length; i++) {
        $img.attr("src", arrayOfImages[i]);
    }
}


In this manner the $(“<img/>”) isn’t being instantiated in every loop - this means a new jQuery object is being formed for every image that you want preloaded.

The for loop could be used instead of the jQuery.each() as it is many times faster than the jQuery.each() loop.