如何用javascript 来获取客户端 屏幕的dpi 值

JavaScript027

如何用javascript 来获取客户端 屏幕的dpi 值,第1张

获取PPI:

function js_getDPI() {

var arrDPI = new Array

if (window.screen.deviceXDPI) {

arrDPI[0] = window.screen.deviceXDPI

arrDPI[1] = window.screen.deviceYDPI

}

else {

var tmpNode = document.createElement("DIV")

tmpNode.style.cssText = "width:1inheight:1inposition:absoluteleft:0pxtop:0pxz-index:99visibility:hidden"

document.body.appendChild(tmpNode)

arrDPI[0] = parseInt(tmpNode.offsetWidth)

arrDPI[1] = parseInt(tmpNode.offsetHeight)

tmpNode.parentNode.removeChild(tmpNode)

}

return arrDPI

}

window.onload=function(){

alert("当前屏幕PPI "+js_getDPI())

}

就把那段script加到head之间。<script type="text/javascript">if(screen.width<=1240){document.write("<link rel='stylesheet' type='text/css' href='css/index_1240.css' />")}</script>screen.width<=1280 你的1024肯定是成立的,当然会用你下边载入的CSS样式来渲染页面。