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; 
7
  • What is the error shown? Commented Apr 12, 2019 at 8:09
  • No error in console, but onerror is always triggered. Open image url manually on browser address bar result in image successfully loaded. Commented Apr 12, 2019 at 8:11
  • Can you show what the onerror is called with when on iOS? Commented Apr 16, 2019 at 15:47
  • Couldn't replicate this Commented Apr 17, 2019 at 6:32
  • add console.log(arguments); inside the onerror function and show us whats printed. Commented Apr 18, 2019 at 1:16

1 Answer 1

4
+50

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

Sign up to request clarification or add additional context in comments.

1 Comment

I will try as soon as possible

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.