Reputation: 709
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)
Upvotes: 0
Views: 2486
Reputation: 709
I had a global scope variable called URL without value, so window.URL referred to it.
Upvotes: 1
Reputation: 717
I would imagine that you should check the value of your parameter b
to ensure it indeed does have value. Sounds to me that it does not and therefore will cause window.URL
to be undefined during construction.
It doesn't sound like you are new to this so I won't sit here and tell you how to debug that but I think that is the best place to look, from what I can see here your code looks as to be expected.
Note: What Mark Dee said is very well a valid option as well, you should make sure that the Document is ready and then try your function see if this helps.
jQuery
$(document).ready(function() {
//CODE HERE
});
Old JavaScript
window.onload = function ()
{
//CODE HERE
}
Upvotes: 0