Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting image src dynamically using JavaScript failed on iOS

I have following JavaScript code

var aimg = new Image();
aimg.crossOrigin = 'Anonymous';
aimg.onload = function () {
    //execute some code when image loaded
};
aimg.onerror = function () {
    //execute some code when image failed to load
};
aimg.src = someExistedImageUrl;

running on Chrome, Firefox on Linux desktop and Android devices, onload is correctly triggered. But in iOS, onerror is always triggered eventhough image exists and coming from same origin.

Why above code failed to load image in iOS?

Update

I add following code as suggested but does not work. The image is relatively small in size, less than 80 KB.

aimg.src = null;
like image 984
Zamrony P. Juhara Avatar asked Sep 06 '25 03:09

Zamrony P. Juhara


1 Answers

Maybe it's a cache problem. Try this and see whether it's working or not;

var aimg = new Image();
aimg.crossOrigin = 'Anonymous';
aimg.onload = function () {
    //execute some code when image loaded
};
aimg.onerror = function () {
    //execute some code when image failed to load
};
aimg.src = null;
aimg.src = someExistedImageUrl;

In addition, check you image size.

JavaScript allocations are also limited to 10 MB. Safari raises an exception if you exceed this limit on the total memory allocation for JavaScript.

For more information, you could check This question

like image 115
Reza Avatar answered Sep 07 '25 21:09

Reza