Created
March 25, 2012 17:44
-
-
Save RaVbaker/2198549 to your computer and use it in GitHub Desktop.
Retina replace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// retina_replace.js - Maciej Nowakowski <[email protected]> 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