HTML DOM Storage getItem() 메소드는 주어진 키 이름을 전달하여 스토리지 객체를 얻는 데 사용됩니다. 키의 값을 반환하고 해당 이름의 키가 없으면 NULL이 반환됩니다.
구문
다음은 Storage getItem() 메서드의 구문입니다. -
localStorage.getItem(keyname);
또는
sessionStorage.getItem(keyname);
여기서 keyname은 string형으로 획득할 아이템의 이름을 나타냅니다.
예시
HTML DOM Storage getItem() 메서드에 대한 예를 살펴보겠습니다. -
<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Storage getItem() method example</h1>
<p>Create a localStorage item with the name CLICKS to count the number of clicks on the create button by clicking the below button</p>
<button onclick="createItem()">CREATE</button>
<p>Get the CLICKS item value by clicking the below button</p>
<button onclick="showItem()">Display</button>
<p id="Sample"></p>
<script>
var y=1;
function createItem() {
document.getElementById("Sample").innerHTML="localStorage Item has been created with name CLICKS";
localStorage.visits = y;
y++;
}
function showItem() {
var x = localStorage.getItem("visits");
document.getElementById("Sample").innerHTML = "The total no. of clicks are : "+x;
}
</script>
</body>
</html> 출력
이것은 다음과 같은 출력을 생성합니다 -

CREATE 버튼 클릭 시 -

"디스플레이" 버튼 클릭 시 -
