admin管理员组文章数量:1134247
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");
}
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");
}
Share
Improve this question
edited Dec 1, 2016 at 10:40
Kristen Grote
2,77730 silver badges43 bronze badges
asked Sep 10, 2012 at 15:31
Oto ShavadzeOto Shavadze
42.7k55 gold badges164 silver badges245 bronze badges
1
- 6 possible duplicate of jQuery callback on image load (even when the image is cached) – Fabrício Matté Commented Sep 10, 2012 at 15:31
6 Answers
Reset to default 210As you're generating the image dynamically, set the onload
property before the src
.
var img = new Image();
img.onload = function () {
alert("image is loaded");
}
img.src = "img.jpg";
Fiddle - tested on latest Firefox and Chrome releases.
You can also use the answer in this post, which I adapted for a single dynamically generated image:
var img = new Image();
// 'load' event
$(img).on('load', function() {
alert("image is loaded");
});
img.src = "img.jpg";
Fiddle
If the src is already set then the event is firing in the cached case before you even get the event handler bound. So, you should trigger the event based off .complete
also.
code sample:
$("img").one("load", function() {
//do stuff
}).each(function() {
if(this.complete || /*for IE 10-*/ $(this).height() > 0)
$(this).load();
});
There are two possible solutions for these kind of situations:
- Use the solution suggested on this post
Add a unique suffix to the image
src
to force browser downloading it again, like this:var img = new Image(); img.src = "img.jpg?_="+(new Date().getTime()); img.onload = function () { alert("image is loaded"); }
In this code every time adding current timestamp to the end of the image URL you make it unique and browser will download the image again
I have met the same issue today. After trying various method, I realize that just put the code of sizing inside $(window).load(function() {})
instead of document.ready
would solve part of issue (if you are not ajaxing the page).
My improvement:
document.addEventListener("DOMContentLoaded", function () {
let el = document.getElementsByTagName("img")
Object.values(el).forEach(function (el) {
var img = new Image();
img.onload = function () {
el.style.opacity = 1;
}
img.src = el.src
})
});
.
<style>
img {
opacity: 0;
transition: opacity .4s cubic-bezier(.25, .45, .45, .95);
}
</style>
I found that you can just do this in Chrome:
$('.onload-fadein').each(function (k, v) {
v.onload = function () {
$(this).animate({opacity: 1}, 2000);
};
v.src = v.src;
});
Setting the .src to itself will trigger the onload event.
本文标签: javascriptimageonload event and browser cacheStack Overflow
版权声明:本文标题:javascript - image.onload event and browser cache - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736814660a1954040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论