HTML DOM localStorage 속성을 사용하면 사용자가 나중에 참조할 수 있도록 로컬 브라우저 자체에 키-값 쌍을 저장할 수 있습니다. 키-값 쌍은 명시적으로 제거/삭제될 때까지 손실되지 않습니다.
구문
다음은 구문입니다 -
Window.localStorage
여기서 localStorage는 다음과 같은 속성/메서드를 가질 수 있습니다. -
- setItem('키','값 ')
- getItem('키')
- removeItem('키')
localStorage 속성의 예를 살펴보겠습니다. -
예시
<!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="Store Key" onclick="storeData()">
<input type="button" value="Delete Key" onclick="clearData()">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var extStyle = document.getElementById("extStyle");
function clearData(){
localStorage.clear();
divDisplay.textContent = 'Succesfully Deleted';
}
function storeData(){
localStorage.setItem('PassKey','ja12ertplo');
divDisplay.textContent = 'Succesfully Stored';
}
</script>
</body>
</html> 출력
'키 저장'을 클릭한 후 버튼 -

'키 삭제'를 클릭한 후 버튼 -
