Somehow I am unable to fire the onerror event or get the code in the onerror to work. This code is an attempt to collect a sequence of images in an array. I am using onerror event in order to mark the end of sequence and thus terminate the loop.

//definition of Slide object
function Slide(inpImage, imgCaption) {
    this.inpImage = inpImage;
    this.imgCaption = imgCaption;
}

window.onload = function() {
        var defaultImage = img_slide.src; //This is a default file
        //basically getting the file path and name of next image in the sequence
        var nextImage = f_nextFile(f_path(img_slide.src),f_serial(img_slide.src)); 
        //An array object which stores Slide Objects
        arr_slides.push(new Slide(img_slide,img_slide.src)); 
        while (defaultImage != nextImage){ //code to populate arr_slides array
            var imgObj = new Image();
            imgObj.src = nextImage;
            arr_slides.push(new Slide(imgObj,nextImage));
            //get the next image in the sequence of images
            nextImage = f_nextFile(f_path(nextImage),f_serial(nextImage));
            testImage = function(nextImage,defaultImage){
                            var tester=new Image();
                            tester.onerror = function () {
                                                nextImage = defaultImage;
                                            };
                            tester.onload = function () {
                                                //do nothing
                                            };
                            var loadNext = function() {
                                                tester.src=nextImage;
                                           };
                            loadNext(); 
            };
            testImage(nextImage,defaultImage);
        }
        alert("The number of images in array : " + arr_slides.length);
     };

Try a syntax error type of error in your code? Add something like alert('intend to be error' which would trigger the error of javascript somewhere in your script that it will get call while it is in the loop or so.

You need to try your code on IE first. If it works there, and it most probably will.
the problem is the brwoser you are using.

Meaning, the [img][src] attribute has been flagged.
You will most probably be forced to use some sort of a hidden iframe to actually load images before you are going to be able to trigger 'the image not fund' error on them.

Funny thing is whenever i am inserting alert messages inside the onerror section, everything works. And whenever, I remove those alert messages, it starts giving me problem.

I have already tried it in IE, it gives the same problem.

Funny thing is whenever i am inserting alert messages inside the onerror section, everything works. And whenever, I remove those alert messages, it starts giving me problem.

I have already tried it in IE, it gives the same problem.

that doesn't mean that everything works,
it means that you are invoking the loadNext command arbitrarily during the loop.

What you need is the initiator followed by:
"testerImage" supplied with "onLoad(loadNext)" and "onError("finish -feed the slider")" handlers by default; where the "loadNext" feeds the new image source from your array each time it is invoked.

You don't need the while loop at all.

A simplified illustration of what i meant:

window.onload = function(){
   var i = 0, srcArray, imgTest;
   srcArray = "mg1.jpg,img2.jpeg,img3.jpg,imgx.jpg".split(",");

   initiator: newImage();

   function newImage(){
         imgTest = new Image();
         imgTest.onload = loadNext;
         imgTest.onerror = finishSeq;
         imgTest.src = srcArray[i]}

   function loadNext(){ imgSeq.appendChild( imgTest );
                        i++; newImage();
                        };
   function finishSeq(){ slide(imgSeq) };
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.