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

คุณสมบัติ length ของ HTML DOM Storage


คุณสมบัติ length ของ HTML DOM Storage ใช้สำหรับดึงจำนวนรายการที่อยู่ภายในออบเจ็กต์ storage ของเบราว์เซอร์ ออบเจ็กต์ storage สามารถเป็น localStorage หรือ sessionStorage

ไวยากรณ์

ไวยากรณ์สำหรับ:

คุณสมบัติ length โดยใช้ localStorage object:

localStorage.length;

คุณสมบัติ length โดยใช้ sessionStorage object:

sessionStorage.length;

ตัวอย่าง

มาดูตัวอย่างสำหรับคุณสมบัติ length ของ Storage:

<!DOCTYPE html> <html> <body> <h1 style="text-align:center">Storage length property example</h1> <p>ดึงจำนวนรายการเก็บใน local storage object โดยคลิกปุ่มด้านล่าง</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>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้:

คุณสมบัติ length ของ HTML DOM Storage

เมื่อคลิก GET NUMBER:

คุณสมบัติ length ของ HTML DOM Storage