4

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;
Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
  • What is the error shown? – briosheje Apr 12 '19 at 08:09
  • No error in console, but `onerror` is always triggered. Open image url manually on browser address bar result in image successfully loaded. – Zamrony P. Juhara Apr 12 '19 at 08:11
  • Can you show what the `onerror` is called with when on iOS? – wojonatior Apr 16 '19 at 15:47
  • Couldn't replicate this – Towkir Apr 17 '19 at 06:32
  • add `console.log(arguments);` inside the `onerror` function and show us whats printed. – luiscla27 Apr 18 '19 at 01:16
  • **its may cause by redirection issue, request may return redirection status(3×× Redirection) 300 Multiple Choices 301 Moved Permanently 302 Found 303 See Other 304 Not Modified 305 Use Proxy 307 Temporary Redirect (you can inspect this in Firefox developer tool) **another possibility is case insensitive / unsupported character in url ,or invalid /unsupported mime type issue – Sajan Apr 21 '19 at 08:11
  • @LuisLimas console.log(event) output gives output similar to https://stackoverflow.com/questions/40572712/javascript-img-src-onerror-event-get-reason-of-error – Zamrony P. Juhara Apr 22 '19 at 02:26

1 Answers1

4

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

Reza
  • 3,473
  • 4
  • 35
  • 54