When using picture, source and srcset how check which src was loaded? (img.src is empty)
Ad
I'm using the picture
element with source
's to choose which image to load. And while I can add a load
listener, I cannot figure out which image was loaded as the img
tag's src
attribute and property are both empty, even when loaded!
<picture>
<source srcset="images/test1.png" media="(min-width: 64em)">
<source srcset="images/test2.png" media="(max-width: 63.99em)">
<!-- This will alert an empty string "" -->
<img srcset="images/test.png" alt="" onload="alert( this.src );">
</picture>
How do I determine which image was loaded?
Ad
Answer
Ad
In modern browsers that implement this, there appears to be a new property: currentSrc
. In the image.onload, you can check for this. In older browsers, it will use src
.
img.onload = function()
{
//Old browser
if ( typeof img.currentSrc === "undefined" ) console.log( img.src );
//Modern browser
else console.log( img.currentSrc );
}
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad