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

ลบ Web Storage อย่างไร?

การเก็บข้อมูล敏感บนเครื่องท้องถิ่นอาจอันตรายและปล่อยช่องโหว่ด้านความปลอดภัย Session Storage Data จะถูกลบโดยเบราว์เซอร์ทันทีหลัง session จบสิ้นสุด

เพื่อล้าง local storage setting ให้เรียก localStorage.remove('key') โดย 'key' คือ key ของค่าที่ต้องการลบ หากต้องการล้างทั้งหมด ให้เรียก localStorage.clear()

ตัวอย่าง

<!DOCTYPE HTML>
<html>
  <body>
    <script>
      localStorage.clear();
      // Reset number of hits.
      if (localStorage.hits) {
        localStorage.hits = Number(localStorage.hits) + 1;
      } else {
        localStorage.hits = 1;
      }
      document.write("Total Hits: " + localStorage.hits);
    </script>
    <p>Refreshing the page would not increase hit counter.</p>
    <p>Close the window and open it again and check the result.</p>
  </body>
</html>