↓↓↓sessionStorage(ブラウザ閉じるまで値を保持)↓↓↓
function func_ss() {
var ss_message = document.getElementById("ss_message").value;
ss_message = "sessionStorageには「" + ss_message + "」が保存されています";
sessionStorage.setItem('ss_save', ss_message);
document.getElementById("output_ss_message").innerHTML = ss_message;
}
if(sessionStorage.getItem('ss_save')){
document.getElementById("output_ss_message").innerHTML = sessionStorage.getItem('ss_save');
}
function func_ss_remove() {
sessionStorage.removeItem('key');
document.getElementById("output_ss_message").innerHTML = '';
}
↓↓↓localStorage(ブラウザ閉じても値を保持)↓↓↓
function func_ls() {
var ls_message = document.getElementById("ls_message").value;
ls_message = "localStorageには「" + ls_message + "」が保存されています";
localStorage.setItem('ls_save', ls_message);
document.getElementById("output_ls_message").innerHTML = ls_message;
}
function func_ls_remove() {
localStorage.removeItem('key');
document.getElementById("output_ls_message").innerHTML = '';
}
if(localStorage.getItem('ls_save')){
document.getElementById("output_ls_message").innerHTML = localStorage.getItem('ls_save');
}
【javascript】ブラウザに値を保存するWebStorageを使ってみた
2020/04/19