Wednesday, July 4, 2012

Get screen density with JavaScript using window.devicePixelRatio DOM property

In mobile web app (specially in Android, various screen densities are provided), we can get screen density with JavaScript using window.devicePixelRatio DOM property.

<!doctype html>
<head>
 <title>Mobile-Web-App: Test Javascript</title>
 <meta charset="utf-8">

<script>
function myOnLoad(){
 
 var devicePixelRatio = window.devicePixelRatio;
 eleRatio = document.getElementById("ratio");
 eleRatio.innerHTML = devicePixelRatio;
 
}

window.onload = myOnLoad;
</script> 

</head>
<body>
<h1>Mobile-Web-App: detect device pixel ratio</h1>

devicePixelRatio: <b id="ratio"></b>

</body>
</html>


devicePixelRatio on Galaxy Nexus

devicePixelRatio on desktop

No comments:

Post a Comment