您的位置:首页 > Web前端 > JavaScript

JS兼容的方式来获取浏览器的宽度

2015-07-11 20:32 555 查看
<script type="text/javascript">

//need to wait until onload so body is available
window.onload = function(){

function getWindowWidth(){
if (window.innerWidth){
return window.innerWidth;
} else if (document.documentElement.clientWidth){
return document.documentElement.clientWidth;
} else if (document.body.clientWidth){
return document.body.clientWidth;
}
}

//determine if the browser has Netscape-style plugins
var hasNSPlugins = !!(navigator.plugins && navigator.plugins.length);

//determine if the browser has basic DOM Level 1 capabilities
var hasDOM1 = !!(document.getElementById && document.createElement &&
document.getElementsByTagName);

alert("Window width: " + getWindowWidth());
alert("Supports Netscape plugins? " + hasNSPlugins);
alert("Supports Core DOM 1?

" + hasDOM1);
}
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: