嘻多瞇圖像傳達

2014/10/29

取得螢幕解析度

取得螢幕解析度

javascript寫法

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>取得螢幕解析度</title>
<meta name="viewport" content="width=device-width">

<script>
window.onload = function() {
if(navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1) {
var myscreenwidth = window.screen.width * window.devicePixelRatio;
指定給ios用
} else if(navigator.userAgent.indexOf('Android 2') != -1) {
var myscreenwidth = window.outerWidth;
指定給Androi 2.x
} else {
var myscreenwidth = window.screen.width;
指定給其他ios用
}
document.getElementById("show").innerHTML = myscreenwidth;
}
</script>
</head>

<body>
<div id="show">取得螢幕解析度</div>
</body>

</html>

jQuery寫法

<head>
<meta charset="utf-8">
<title>取得螢幕解析度</title>
<meta name="viewport" content="width=device-width">
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

<script>
$(function() {
if(navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1) {
var myscreenwidth = window.screen.width * window.devicePixelRatio;
} else if(navigator.userAgent.indexOf('Android 2') != -1) {
var myscreenwidth = window.outerWidth;
} else {
var myscreenwidth = window.screen.width;
}
$("#show").html(myscreenwidth);
});
</script>

</head>

<body>
<div id="show">取得螢幕解析度</div>
</body>
</html>


參考資料:HTML5+CSS智慧型手機專用網站設計

沒有留言: