Skip to content

Instantly share code, notes, and snippets.

@RaVbaker
Created March 25, 2012 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RaVbaker/2198549 to your computer and use it in GitHub Desktop.
Save RaVbaker/2198549 to your computer and use it in GitHub Desktop.
Retina replace
// retina_replace.js - Maciej Nowakowski <macnow@gmail.com> Twitter: @macnow - more: http://macnow.pl/ios/retina/
function RetinaReplace(){
function RetinaCheck(el){
var url = el.src.replace(/\.(gif|png|jpg)$/gi, "_2x.\$1");
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("HEAD", url, true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200)
{
el.width = el.clientWidth;
el.height = el.clientHeight;
el.src = url;
}
}
}
xmlhttp.send(null);
}
var RE = document.getElementsByTagName("img");
for (var i = 0; (el = RE[i]) != null; i++) {
RetinaCheck(el);
}
}
onload=function(){
if( window.devicePixelRatio >= 2 ) RetinaReplace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment