Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Detect retina asset in DOM using javascript
function retina(selector) {
var element = document.querySelector(selector);
var images = Array.prototype.slice.apply(element.getElementsByTagName("img"));
var extension = "_2x";
if (window.devicePixelRatio === 2) {
images.forEach(function(img, idx) {
var src = img.getAttribute('src');
img.setAttribute('src', src.replace(/(\.jpg|\.png|\.gif)$/, extension + '$&'));
img.onerror = function(evt) {
img.setAttribute('src', src);

This comment has been minimized.

Show comment Hide comment

ggilder Mar 21, 2012


ggilder commented Mar 21, 2012


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment