Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีหาความสูงและความกว้างภายในของหน้าต่างเบราว์เซอร์ใน JavaScript

window object แทนหน้าต่างเบราว์เซอร์ มีพร็อพเพอร์ตี้ window.innerWidth และ window.innerHeight ให้ขนาดพื้นที่แสดงเนื้อหา (viewport) ในพิกเซล

Inner Width

<html> <body> 
<p id="width"></p> 
<script> 
    var w = window.innerWidth; 
    var x = document.getElementById("width"); 
    x.innerHTML = "Browser inner window width: " + w; 
</script> </body> </html>

ผลลัพธ์

Browser inner window width: 598

Inner Height

<html> <body> 
<p id="height"></p> 
<script> 
    var h = window.innerHeight; 
    var x = document.getElementById("height"); 
    x.innerHTML = "Browser inner window height: " + h; 
</script> </body> </html>

ผลลัพธ์

Browser inner window height: 477