Home > Web Front-end > JS Tutorial > body text

How to Ensure an Onload Event is Triggered for an Image, Even if It\'s Cached?

Barbara Streisand
Release: 2024-10-25 05:49:29
Original
882 people have browsed it

How to Ensure an Onload Event is Triggered for an Image, Even if It's Cached?

Image onload Event and Browser Cache

You want to trigger an alert box when an image has been loaded, regardless of whether the image has been cached or not. The solution is to set the onload property before the src.

var img = new Image();
img.onload = function () {
   alert("image is loaded");
}
img.src = "img.jpg";
Copy after login

This will work on the latest Firefox and Chrome releases. You can also use the answer in another post, which is 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";
Copy after login

This will also work on the latest Firefox and Chrome releases.

The above is the detailed content of How to Ensure an Onload Event is Triggered for an Image, Even if It\'s Cached?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!