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

JavaScript022

如何用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())

}

function $_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

}

使用的时候直接输入$_GETDPI()即可