Asked 12 years, 11 months ago
Viewed 340k times
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");
}
asked Sep 10, 2012 at 15:31
Oto ShavadzeOto Shavadze43.2k5555 gold badges167167 silver badges247247 bronze badges
1As 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";
answered Sep 10, 2012 at 15:41
Fabrício MattéFabrício Matté70.3k2727 gold badges135135 silver badges168168 bronze badges
14If 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();
});
answered Jul 24, 2015 at 19:52
There are two possible solutions for these kind of situations:
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
answered Sep 10, 2012 at 15:36
haynarhaynar6,05177 gold badges3737 silver badges5353 bronze badges
5I 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).
15.8k1313 gold badges106106 silver badges122122 bronze badges
answered Jan 3, 2013 at 6:05
2My 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>
answered Apr 7, 2023 at 20:58
OreoOreo3133 bronze badges
1I 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.
answered Feb 1, 2017 at 8:15
Noah EllmanNoah Ellman17711 silver badge44 bronze badges
Start asking to get answers
Find the answer to your question by asking.
Ask questionExplore related questions
See similar questions with these tags.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4