Phương thức clear() của Local Storage trong HTML DOM được dùng để xóa toàn bộ các mục trong local storage.
Cú pháp
localStorage.clear()
Hoặc
sessionStorage.clear()
Ví dụ
Ví dụ minh họa phương thức LocalStorage clear():
<!DOCTYPE html> <html> <head> <title>LocalStorage clear()</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } table,th,td { border:1px solid black; border-collapse: collapse; margin: 0 auto; } </style> </head> <body> <form> <fieldset> <legend>LocalStorage clear()</legend> <table> <tr> <th>Key</th> <th>Value</th> </tr> <tr> <td>PassKey</td> <td>ja12ertplo</td> </tr> </table> <input type="button" value="Lưu Key" onclick="storeData()"> <input type="button" value="Xóa Key" onclick="clearData()"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); function clearData(){ localStorage.clear(); divDisplay.textContent = 'Succesfully Deleted'; } function storeData(){ localStorage.setItem('PassKey','ja12ertplo'); divDisplay.textContent = 'Succesfully Stored'; } </script> </body> </html>
Kết quả
Sau khi nhấn nút 'Lưu Key':

Sau khi nhấn nút 'Xóa Key':
