Search code examples
javascripthtmlasynchronousfileapi

window.URL is undefined


I have an array being populated with blobs (it works correctly) and I want to convert the Blob to Image as soon as I get the blob.

(function(index) {

    images[index] = new Image();

    xhr.onload = function() {

        blobs[index] = new Blob([this.response], {type: 'image/png'});

        console.log(index + " loaded.");
        console.log(blobs[index]); // It works as expected.

        (function(b) {

            var img = window.URL.createObjectURL(b); // window.URL is undefined.
            console.log(img); // Failed.

        })(blobs[index]);

    };

})(i);

The anonymous function is not called immediately after the console.log, and it throws the error:

TypeError: window.URL is undefined

I'm using Firefox 17, which already features window.URL, so that's not the problem. Actually, I can use window.URL outside this function.

EDIT: I had a global scope variable called URL without value, so window.URL referred to it. (Can't self-answer due to my low reputation)


Solution

  • I had a global scope variable called URL without value, so window.URL referred to it.