I have some simple code where I check an image’s width and height, and if it’s not >= 120×90, it gets a visibility: hidden
put on it.
$(".video-thumbnail").each(function() {
var width = $(this).prop("naturalWidth");
var height = $(this).prop("naturalHeight");
if (width <= 120 && height <= 90) {
$(this).css("visibility", "hidden");
}
});
The problem is this randomly fails on some page reloads (see edit below).
I think it may be a caching or browser loading delay issue.
EDIT
Confirmed sequence of events to reproduce:
- Images are cached in browser
- Modify HTML source
- Navigate to page (not reload)
Any suggestions to run this more reliably? Maybe force the browser to download the image file? Not sure what that would look like.
2
Answers
Posting an alternative method that also works as well as the one marked correct for my future reference:
Calling
$(window).on("load"...
first is the only change. Sometimes images were not loaded with my original code.Sources:
Curious if there are any performance differences between this and the code in the answer marked correct. Behaviors seem to be identical.
A more reliable solution would be to use the HTMLImageElement.decode() that returns a promise which is resolved once the full-resolution image is ready for use, an example of this is below: