UPDATE: it looks like Scott Jehl has already beaten me to the punch, so check out his github project!
So, I was almost asleep, and this idea popped into my head. I want to write it down for two reasons:
- So I don't forget it.
- To see if anyone else agrees, disagrees, or sees any flaws in the idea.
- in a syntax that is logical and
- in a way that can be powered by javascript and
- fails gracefully to non-javascript supporting clients
<image data-alt="Some image" data-width="300px" data-height="300px" id="my_image" class="some_class"> <source src="/images/high-dpi/image.jpg" media="screen and (-webkit-device-pixel-ratio: 2.0)" /> <source src="/images/print-dpi/image.jpg" media="print" /> <source src="/images/regular-dpi/image.jpg" /> <img src="/images/regular-dpi/image.jpg" width="300" height="300" alt="Some image" /> </image> |
With no javascript, nothing but the <img> tag is displayed, and everything is happy.
With javascript, we run a system which turns it into the following (this becomes easier if we can evaluate media queries in JS, but I'm not sure if we can do that, so I'm showing an alternate way):
<style> #my_image { background-image: url('/images/regular-dpi/image.jpg'); background-repeat: no-repeat; background-size: 300px 300px; } @media print { #my_image { background-image: url('/images/print-dpi/image.jpg'); } } @media screen and (-webkit-device-pixel-ratio: 2.0) { #my_image { background-image: url('/images/high-dpi/image.jpg'); } } </style> <image data-alt="Some image" data-width="300px" data-height="300px" id="my_image" style="width: 300px; height: 300px;"> </image> |
And now for the fun part - we can use document.getElementById('my_image').style.backgroundImage to get the right image!
This means that the last and final step then turns into:
<image data-alt="Some image" data-width="300px" data-height="300px" id="my_image"> <img style="width: 300px; height: 300px;" src="/images/high-dpi/image.jpg" alt="Some image" width="300" height="300" /> </image> |
Which should make it happy with screen readers and other similar systems.
This approach has a few distinct advantages:
- It leverages existing tools for defining when to load a particular image (media queries)
- It provides a graceful fallback for no javascript support
- It (hopefully) will be relatively close to whatever is selected for moving forward in a browser integrated solution, as it fits with the patterns already established.
So that's it. That's my master plan. What do you think?