Thuộc tính window.length trả về số lượng iframe (khung nội tuyến) trong cửa sổ hiện tại.
Cú pháp
window.length
Ví dụ
<!DOCTYPE html>
<html>
<style>
body { color: #000; height: 100vh; background-color: #8BC6EC; background-image: linear-gradient(135deg, #8BC6EC 0%, #9599E2 100%); text-align: center; }
.btn { background: #db133a; border: none; height: 2rem; border-radius: 2px; width: 40%; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem auto; }
</style>
<body>
<h1>Demo window.length</h1>
<iframe src="https://www.tutorialspoint.com/"></iframe>
<button onclick="showCount()" class="btn">Số lượng iframe</button>
<div class="show"></div>
<script>
function showCount() {
document.querySelector('.show').innerHTML = 'Số iframe: ' + window.length;
}
</script>
</body>
</html>
Kết quả
Nhấn "Số lượng iframe" → hiển thị "Số iframe: 1" (do có 1 thẻ iframe trong ví dụ).
Lưu ý: Chỉ đếm iframe trực tiếp trong window, không đệ quy vào iframe con (cross-origin bị chặn). Hữu ích cho frame management.