Web StorageăŻăăŚăźăśăźăŽăăźăŤăŤç°ĺ˘ďźăăŠăŚăśďźăŤăăźăżăäżĺăăăăăŽäťçľăżă§ăă ăăźăżăŽäżĺăťä¸ć¸ăăťĺé¤ăťĺ ¨ăŻăŞă˘ăŞăŠăŽćä˝ăŻăJavaScriptă§čĄăăă¨ăă§ăăžăă
Web StorageăĺŠç¨ăăă¨ăăŚăźăśăźăă¨ăŤăŤăšăżăă¤ăşăăăć ĺ ąăćäžăăăă¨ăĺŻč˝ă¨ăŞăăžăă ăăŽäťçľăżăŻăŻăăăźďźHTTP cookieďźă¨ăăäźźăŚăăžăăăăŻăăăźăŤćŻăšăŚäżĺă§ăă厚éă大ăăăăă äžăă°ăăŞăăŠă¤ăłă§ăăŚă§ăă˘ăăŞăąăźăˇă§ăłăĺä˝ăăăăăă ăăŽ ĺż čŚĺĺăŞăăźăżăăŚăźăśăźăŽăăźăŤăŤç°ĺ˘ăŤäżĺăăăă¨ăŞăŠăĺŻč˝ă¨ăŞăăžăă
Web StorageăŤăŻăsessionStorageă¨localStorageăŽďźç¨ŽéĄăŽăšăăŹăźă¸ăç¨ćăăăŚăăžăă ăŠăĄăăăăźďźkeyďźă¨ĺ¤ďźvalueďźăăă˘ăŤăăăăźăżăŽăŞăšăă ăŚăźăśăźăŽăăźăŤăŤç°ĺ˘ăŤäżĺăăkey-valueĺăŽăăźăżäżĺ形ĺźă§ăăçšăŻĺăă§ăăă ăăźăżăŽćĺšćéăŞăŠăç°ăŞăăŽă§çŽçăŤĺżăăŚä˝żăĺăăžăă
ĺĽăŚăŁăłăăŚă§ăŽ ăăźăżĺ ąć |
ăăźăżăŽ ćĺšćé |
ăăźăżé ăŽä¸é |
ăľăźăăźă¸ăŽ ăăźăżé俥 |
|
---|---|---|---|---|
ăŻăăăź | â | ćĺŽćéăžă§ćĺš | 4KB | ăľăźăăźă¸ă˘ăŻăťăšăăăăłăŤćŻĺčŞĺé俥 |
sessionStorage | Ă | ăŚăŁăłăăŚăăżăăéăăăžă§ćĺš | 1ăŞăŞă¸ăłĺ˝ăă5MB | ĺż čŚćăŽăżăšăŻăŞăăăăăŠăźă ăŞăŠă§é俥 |
localStorage | â | ć°¸çśçăŤćĺš |
sessionStorageăŻăăŚăŁăłăăŚăăżăĺä˝ă§ăŽä¸ĺéăăŽăťăăˇă§ăłă§ćĺšăŞăšăăŹăźă¸ă§ăă ăŚăŁăłăăŚăăżăăéăăŚăăéăŽăżăăźăżăäżĺăăăéăăă¨ăăźăżă夹ăăăžăă ĺăăăĄă¤ăłăŽăľă¤ăăĺĽă ăŽăŚăŁăłăăŚă§éăăŚăăĺ ´ĺăŤăŻăăăăăăĺĽăŽsessionStorageă¨ăŞăăžăă ăŻăăăźă¨ăŻç°ăŞăăăŚăŁăłăăŚéă§ăăźăżăĺ ąćăăăăă¨ăŻăăăžăăă
sessionStorageăĺŠç¨ăăä¸äžăćăăŚăżăžăă äžăă°ă2ă¤ăŽăŚăŁăłăăŚă§ĺăăľă¤ăăéăăŞăăčŞçŠşĺ¸ă注ćăăĺ ´ĺă ăŻăăăźă§ăŻč˛ˇăçŠăăć ĺ ąă桡ĺ¨ăăŚăăžăŁăŚăĺăăăŠă¤ăăŽăăąăăăć°ăĽăăăŤ2ć注ćăăŚăăžăĺŻč˝ć§ăăăăžăă
sessionStorageă§ăŻăăăăăăĺĽăŽăťăăˇă§ăłă¨ăŞăăăă ĺĽăŚăŁăłăăŚăŽč˛ˇăçŠăăć ĺ ąă桡ĺ¨ăăŚăăžăăă¨ăăăăžăăă
localStorageăŻăăŞăŞă¸ăłĺä˝ă§ăăźăżăć°¸çśçăŤäżĺăăăšăăŹăźă¸ă§ăă ăŞăŞă¸ăłă¨ăŻăăhttp://www.example.com:80ăăŽăăăŞăăăăăłăŤ://ăăĄă¤ăłĺ:ăăźăçŞĺˇăăŽăă¨ă§ăă ăŞăŞă¸ăłăĺăă§ăăă°ĺĽăŚăŁăłăăŚă§ăăăźăżăĺ ąćă§ăăăăŠăŚăśăçľäşăăŚăăăźăżăŻĺ¤ąăăăžăăă ăăźăżäżĺéăŽä¸éăŻăăŠăŚăśă¸ăŽć¨ĺĽ¨ĺ¤ă1ăŞăŞă¸ăłĺ˝ăă5MBă¨ăăăŚăăă ăŻăăăźăŽ4KBăŤćŻăšăă¨ăă大ăăŞăăźăżăäżĺă§ăăăăăŤăŞăŁăŚăăžăă
localStorageăĺŠç¨ăăä¸äžăćăăŚăżăžăă äžăă°ăăŚă§ăăĄăźăŤăŤăăăăĄăźăŤăŽéĺ俥ăăźăżăŽăăăŞćŻčźç大ăăŞăăźăżăŽĺ ´ĺă ăŻăăăźă§ăŻäżĺă§ăăăăźăżéăŤä¸éăăăăăăăăšăŚăŽăăźăżăăăźăŤăŤç°ĺ˘ăŤäżĺăăăă¨ăŻĺ°éŁă§ăă ăžăăăŻăăăźă§ăŻăŚăźăśăźăă˘ăŻăťăšăă庌ăŤăăźăżăăľăźăăźăŤé俥ăăăăăăăťăăĽăŞăăŁăŽčŚłçšăăă弽ăžăăăăăžăăă
localStorageă§ăŻăăăźăŤăŤç°ĺ˘ăŤ5MBăžă§ăŽăăźăżăć°¸çśçăŤäżĺă§ăă ăŚăźăśăźăŽă˘ăŻăťăšăŽĺşŚăŤăăźăżăăľăźăăźăŤé俥ăăăăă¨ăăăăžăăă ăŚă§ăăĄăźăŤăŽäžă§č¨ăă°ălocalStorageăŞăéĺ俥ăăźăżăăăźăŤăŤç°ĺ˘ăŤäżĺă§ăă ăŞăăŠă¤ăłăŚă§ăă˘ăăŞăąăźăˇă§ăłă¨çľăżĺăăăăă¨ă§ăăŞăăŠă¤ăłă§ăŽăĄăźăŤé˛čŚ§ăŞăŠăĺŻč˝ă¨ăŞăă§ăăăă
Web Storageă§ćĺŽă§ăăĺ˝äť¤ăŻăăăźăżăŽäżĺăťäżĺăăăăźăżăŽĺĺžăťćĺŽăăźăŽĺ¤ĺé¤ăťĺ ¨ăŻăŞă˘ăŽăăăăă§ăă ăăăăăŽĺ˝äť¤ăćĺŽăăăĄă˝ăăăŻäťĽä¸ăŽéăă§ăă ăĄă˝ăăăťĺąć§ăŻăsessionStorageă¨localStorageă§ĺ ąéă¨ăŞăăžăă
getItem(key) âŚâŚ keyăŤĺŻžĺżăăvalueăĺĺžăă
setItem(key, value) âŚâŚ keyă¨valueăăă˘ăŤăăŚăăźăżăäżĺăă
removeItem(key) âŚâŚ keyăŤĺŻžĺżăăvalueăĺé¤ăă
clear() âŚâŚ ăăźăżăăăšăŚăŻăŞă˘ăă
äžăă°ăuseridă¨ăăăăźďźkeyďźă¨ă1111ă¨ăăĺ¤ďźvalueďźăăă˘ăŤăăŚäżĺăăĺ ´ĺăŤăŻă 䝼ä¸ăŽăăăŤćĺŽăăžăă
//ĺ¤ć°storageăŤsessionStorageăć źç´
var storage = sessionStorage;
//useridă¨ăăăăźăŤ1111ă¨ăăĺ¤ăĺ˛ăĺ˝ăŚăŚäżĺ
storage.setItem('userid', '1111');
//ĺžăăvalueăĺ¤ć´ăăŚăťăăăç´ăă°ä¸ć¸ăäżĺăăă
storage.setItem('userid', '2222');
//useridă¨ăăăăźăŽĺ¤ăĺĺžăăďź2222ăčżăăŻăďź
storage.getItem('userid');
//useridă¨ăăăăźăŽĺ¤ăĺé¤ăă
storage.removeItem('userid');
//ăšăăŹăźă¸ăŤäżĺăăăŚăăăăźăżăăăšăŚăŻăŞă˘ăă
storage.clear();
ĺ°ăä¸ăŽăľăłăăŤă§ăŻsessionStorageă¨ăăŚăăźăżăäżĺăăŚăăžăăă ăsessionStorageăăŽé¨ĺăălocalStorageăăŤĺ¤ăăă° localStorageă¨ăăŚăăźăżăäżĺăăăžăă
sessionStorageă¨localStorageăŤăăźăżăŽčż˝ĺ ăťä¸ć¸ăăťĺé¤ăťĺ ¨ăŻăŞă˘ăŞăŠăŽ ä˝ăăăŽć´ć°ăăăă¨storageă¨ăăă¤ăăłăăçşçăăžăă storageă¤ăăłăăŤăŻă䝼ä¸ăŽĺąć§ăç¨ćăăăŚăăžăă
ĺąć§ĺ | čżăĺ¤ |
---|---|
key | ć´ć°ăăăkey |
oldValue | ć´ć°ăăăkeyăŽĺ¤ăvalue |
newValue | ć´ć°ăăăkeyăŽć°ăăvalue |
url | keyăć´ć°ăăăćć¸ăŽURL |
storageArea | ć´ć°ăŤăăŁăŚĺ˝ąéżăĺăăStorageăŞăă¸ă§ăŻă |
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset=utf-8>
<script>
//ĺ¤ć°storageăŤsessionStorageăć źç´
var storage = sessionStorage;
//ăăźăżăäżĺăă
function set() {
var k = document.getElementById("k").value;
var v = document.getElementById("v").value;
storage.setItem(k, v);
show_result();
}
//ăăźăżăăŻăŞă˘ăă
function cle() {
storage.clear();
show_result();
}
//äżĺăăăŚăăăăźăżăăŞăšă襨示ăă
function show_result() {
var result = "";
//äżĺăăăŚăăăăźăżăŽć°ă ăăŤăźă
for(var i=0; i<storage.length; i++){
//içŞçŽăŽăăźăĺĺž
var k = storage.key(i);
//ăăźă¨ĺ¤ăăłăăłďźďźďźĺşĺăăŽăăăšăăŤăă
result += k + "ďź" + storage.getItem(k) + "<br>";
}
//ä¸ăŽăŤăźăă§ä˝ćăăăăăăšăă襨示ăă
document.getElementById("show_result").innerHTML = result;
}
</script>
<title>Web StorageăăľăłăăŤ</title>
</head>
<body>
<p>
ăăźďź<input id="k" type="text">
ĺ¤ďź<input id="v" type="text">
<input type="button" value="ăšăăŹăźă¸ăŤäżĺ" onClick="set()">
</p>
<p>
<div id="show_result"></div>
<input type="button" value="ăšăăŹăźă¸ăăŻăŞă˘" onClick="cle()">
</p>
</body>
</html>
Web StorageăŤăŻă䝼ä¸ăŽăĄă˝ăăăťĺąć§ăç¨ćăăăŚăăžăă
length âŚâŚ äżĺăăăŚăăăăźăżăŽć°ăčżă
key(n) âŚâŚ äżĺăăăŚăănçŞçŽăŽkeyăčżă
getItem(key) âŚâŚ keyăŤĺŻžĺżăăvalueăĺĺžăă
setItem(key, value) âŚâŚ keyă¨valueăŽăă˘ă§ăăźăżăäżĺăă
removeItem(key) âŚâŚ keyăŤĺŻžĺżăăvalueăĺé¤ăă
clear() âŚâŚ ăăźăżăăăšăŚăŻăŞă˘ăă