Skip to content

Instantly share code, notes, and snippets.

@andr3
Created April 3, 2013 14:49
Show Gist options
  • Save andr3/5301866 to your computer and use it in GitHub Desktop.
Save andr3/5301866 to your computer and use it in GitHub Desktop.
Sometimes I have to explain this logic, this gist makes it easier. ;)
// Quick implementation of ResponsiveEnhance by Josh Emerson
// Original implementation: https://github.com/joshje/Responsive-Enhance
// <img src="sd.jpg" data-hd="hd.jpg">
function quickUpgrade (obj) {
var tmp = new Image(); // prepare a loose image to make the request & cache it
var newsrc = obj.getAttribute('data-hd'); // grab the URL of hd version
if (newsrc) { // make sure it's there
tmp.onload = function () { // do this only when new img is loaded & cached
obj.src = newsrc; // switcheroo
};
tmp.src = newsrc; // start the request (while leaving sd version visibile/in DOM)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment