0

Given the following Java Script running on Firefox, I get the alert after the image is displayed on the screen, as it should:

document.getElementById(c).children[0].src = "i/" + table16[c] + ".png";
alert(c);

However, if I run the same script on Chrome, I get the alert before the image is displayed on the screen. This is causing problems. Why does this happen? How can I force Chrome to fully execute the first line before processing the second line?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
ca ac
  • 1
  • Create a jsfiddle demo – Tushar May 10 '15 at 10:09
  • You may find possible solutions in here: http://stackoverflow.com/questions/280049/javascript-callback-for-knowing-when-an-image-is-loaded – Igal S. May 10 '15 at 13:49
  • Thanks. I checked your link and saw there the img.complete property mentioned. – ca ac May 10 '15 at 14:56
  • So I removed the alert(c); line and added the following line: do {console.log (document.getElementById(c).children[0].complete);} while (!document.getElementById(c).children[0].complete); – ca ac May 10 '15 at 14:57
  • Now Firefox still runs ok. it loads the image, reports 'true' on the console a proceeds correctly with the processing. Chrome just keeps reporting 'false' on the console and gets stuck - I have to kill it in the task manager. The problem seems to be that, while Firefox waits for the image to be loaded before processing any more instructions in the script, Chrome will proceed to process code even before the image is fully loaded. But the image will never load while Chrome is processing anything. So, what I need is to get Chrome to stop processing code before the image is loaded. Any ideas? – ca ac May 10 '15 at 15:01
  • what's a jsfiddle demo ? – ca ac May 10 '15 at 19:34
  • [JsFiddle](http://jsfiddle.net/) – glend May 12 '15 at 10:37

0 Answers0