开发者

img.onerror does not seem to work for IE8

开发者 https://www.devze.com 2023-04-08 15:47 出处:网络
I am trying to load an image from a url to check internet connectivity. When no internet connection, it should display a dojo warning dialog. This works for Firefox but does not for IE8.

I am trying to load an image from a url to check internet connectivity. When no internet connection, it should display a dojo warning dialog. This works for Firefox but does not for IE8.

Following is the code snippet:

var img = new Image();
img.src = userGuideUrl1_img + '?' + (new Date).getTime();
img.onload = function() {
        window.open(userGuideUrl1);
}
img.onerror = function() {
    dojo.addOnLoad(warningDialogFunc);
}

Here warningDialogFunc is a dojo object. Any th开发者_开发知识库oughts? Thanks


Could it be that the page is already loaded by the time the img.onerror handler is executed, and IE doesn't rexecute the function for the dojo.addOnLoad(warningDialogFunc)?

Try changing

img.onerror = function() {
    dojo.addOnLoad(warningDialogFunc);
}

to simply:

img.onerror = function() {
  warningDialogFunc();
}


You have to set up the handler before you set the source of image, when you change the src attribute, IE will try to download the image and trigger the events.

var img = new Image();
img.onload = function() {
        window.open(userGuideUrl1);
}
img.onerror = function() {
    dojo.addOnLoad(warningDialogFunc);
}
img.src = userGuideUrl1_img + '?' + (new Date).getTime();  // Trigger image download and the handlers.
0

精彩评论

暂无评论...
验证码 换一张
取 消