ビューポート(ウィンドウ)の横幅を取得する

書式

document.viewport.getWidth()

説明

ビューポート(ウィンドウ)の横幅を取得するにはdocument.viewport.getWidth()を使います。戻り値はピクセルになります。

サンプルコード [実行]

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Sample</title>
<link rel="stylesheet" href="main.css" type="text/css" media="all">
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript"><!--
window.onload = function (){
var w = document.viewport.getWidth();
alert("横幅:"+w);
}
// --></script>
</head>
<body>
<h1>ビューポート(viewport)の横幅を表示</h1>
</body>
</html>