When you're using srcset like this:
<img src="example-src.jpg" srcset="example-1x.jpg 1x, example-2x.jpg 2x" />
Or using a picture tag like this:
<picture>
<source media="(min-width: 64em)" src="high-res.jpg">
<source media="(min-width: 37.5em)" src="med-res.jpg">
<source src="low-res.jpg">
<img src="fallback.jpg" />
</picture>
Is it possible to find out what URL the browser decided to use? Checking the src
attribute of the <img>
tag itself doesn't actually tell you which image was loaded.
(Sorry if this is a duplicate. I can't find the solution to this anywhere.)
srcset defines the set of images we will allow the browser to choose between, and what size each image is. Each set of image information is separated from the previous one by a comma.
Yes it's valid. The src is used as a fallback if the browser doesn't support srcset.
Definition and Usage. The srcset attribute specifies the URL of the image to use in different situations. This attribute is required when <source> is used in <picture> .
Responsive images are the set of techniques used to load the right image based on device resolution, orientation, screen size, network connection, and page layout. The browser should not stretch the image to fit the page layout, and loading it shouldn't result in time & bandwidth wastage.
You can use currentSrc
in at least some browsers, (though I don't know which.)
document.getElementById('myImage').currentSrc;
Or...
jQuery('#myImage').prop('currentSrc');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With