คุณสมบัติความยาวพื้นที่เก็บข้อมูล HTML DOM ใช้สำหรับรับจำนวนรายการที่มีอยู่ในวัตถุที่เก็บข้อมูลของเบราว์เซอร์ วัตถุหน่วยเก็บข้อมูลอาจเป็นวัตถุ localStorage หรือวัตถุ sessionStorage ได้
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์สำหรับ −
คุณสมบัติความยาวของการจัดเก็บโดยใช้วัตถุ localStorage -
localStorage.length;
คุณสมบัติความยาวหน่วยเก็บข้อมูลโดยใช้วัตถุ sessionStorage
sessionStorage.length;
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับคุณสมบัติความยาวการจัดเก็บ -
<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Storage length property example</h1>
<p>Get how many storage items are stored in the local storage object by clicking the below button</p>
<button onclick="itemNum()">GET NUMBER</button>
<p id="Sample"></p>
<script>
function itemNum() {
var num = localStorage.length;
document.getElementById("Sample").innerHTML = "Number of storage items are "+num;
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

เมื่อคลิกที่ GET NUMBER -
