mobile safari - reliable callback for when Image loads?

非 Y 不嫁゛ 提交于 2019-12-11 18:56:01

问题


I try to load an image as such:

var img = new Image();
img.src = 'mars.png';
img.onLoad = callback;


function callback(){
    // doesnt fire
       alert("loaded");
}

the callback never fires, whats the workaround?


回答1:


You MUST define the onload BEFORE you change the src and event handlers are lowercase so it is spelled onload

var img = new Image();
img.onload = callback;
img.src = 'mars.png';

function callback(){
  alert("loaded");
}

or as I prefer it

var img = new Image();
img.onload = function(){
  alert("loaded");
}
img.src = 'mars.png';



回答2:


Have you tried these?

var img = new Image();
img.src = 'mars.png';
img.onLoad = function(){
    // doesnt fire
       alert("loaded");
};



回答3:


mplungjan is right you can use these instead:

img.onload = function(){
    // doesnt fire
       alert("loaded");
};

with lower l in onLoad



来源:https://stackoverflow.com/questions/11805945/mobile-safari-reliable-callback-for-when-image-loads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!