I want to create an alert box after an image is loaded, but if the image is saved in the browser cache, the .onload
event will not be fired.
How do I trigger an alert when an image has been loaded regardless of whether the image has been cached or not?
var img = new Image();
img.src = "img.jpg";
img.onload = function () {
alert("image is loaded");
}
ベストアンサー1
画像を動的に生成しているので、onload
の前に プロパティを設定しますsrc
。
var img = new Image();
img.onload = function () {
alert("image is loaded");
}
img.src = "img.jpg";
フィドル- 最新の Firefox および Chrome リリースでテスト済み。
この回答も使用できます役職これを、動的に生成された単一の画像に適応させました。
var img = new Image();
// 'load' event
$(img).on('load', function() {
alert("image is loaded");
});
img.src = "img.jpg";