localStorageã®æåã¨ç°¡åãªã©ããã¼
[追è¨]
ãã¹ãã®ã½ã¼ã¹ã ããè¦ãããæ§ã«ãgistã«å¼µãã¾ããã
gist:542451
localStorageã使ã£ã¦ã¡ãã£ã¨ãã£ã¦ã¿ãããã¨ãæãã®ã§ãã¾ãã¯localStorageãè²ã 使ã£ã¦è¦ããã¨æã£ãã®ã§ãããæã£ã以ä¸ã«ãã©ã¦ã¶ãã¨ã®æåã«å·®ããã£ã¦ãã¤ãã³ãã©ãããã ã å¤ãåãåºãã ãã§ããè²ã æ°ãã¤ããå¿ è¦ããããã¨ãåããã¾ããã
以ä¸ã¯ãæå ã® Mac ã«æã£ã
ã®ç°å¢ã§è©¦ããçµæã§ãã
æ ¼ç´åºæ¥ããã¼ã¿
W3Cã®ä»æ§ã§ã¯JSã®ãªãã¸ã§ã¯ãã§ããã°ä¸éãæ ¼ç´åºæ¥ãããã«å®ãã¦ãããããã§ãããç¾å¨ã¯åãªãæååããå
¥ããªãç©ãå¤ãããã§ãã
ãªã®ã§ããªãã¸ã§ã¯ããæ ¼ç´ããããã«ã¯ãJSONãã·ãªã¢ã©ã¤ãºãã¦å
¥ããå½¢ã«ãªãã¨æãã¾ãã
Native JSON ã使ã£ã¦ããã¨ãããªæãã
//ã·ãªã¢ã©ã¤ãºãã¦æ ¼ç´ var str = JSON.stringify({"hoge":"fuga"}); localStorage.setItem(key,str); //åãåºãã¦ã§ã·ãªã¢ã©ã¤ãº var tmp = localStorage.getItem(key); var obj = JSON.parse(tmp);
ãããããããæ¯åããã®ã¯é¢åãããã®ã§ã常ã«ã·ãªã¢ã©ã¤ãºã¨ãã·ãªã¢ã©ã¤ãºããããã«ã©ãããæ¸ããã¨æã£ãããã§ãã
æ ¼ç´ã¨åå¾
ã§ãæ¸ãã¦ãããè²ã
å®è£
ã®å·®ç°ããããã¨ãåãã£ã¦æ¥ã¾ããã
ä¾ãã°ãå
ã»ã©ã®æ ¼ç´ã¨åå¾ã¯ã以ä¸ã®ä¸ã¤ã®ããæ¹ãæãã¨ã®ãã¨ã
//ããããã£âã¢ã¯ã»ã¹ localStorage.key = value; var value = localStorage.key; //ããã·ã¥ã¢ã¯ã»ã¹ localStorage["key"] = value; var value = localStorage["key"]; //ã¡ã½ãã localStorage.setItem(key, value); var value = localStorage.getItem(key);
ããããããFireFoxã§ã¯å
¨ã¦æ£å¸¸ã«åãããã§ããã
ããããã£ã¼ã¨ããã·ã¥ã®å ´åãChrome,Safari ã§ã¯lengthããããã£ã¼ãæ´æ°ãããªãã
ã¾ããåå¨ããªããã¼ã¸ã®ã¢ã¯ã»ã¹ãä»æ§ã§ã¯nullãªã®ã«å¯¾ããChrome,Safariã§ã¯undefinedã«ãªãããã§ãã
ã¤ã¾ããã¢ã¯ã»ã¹ã¯é¢åã§ãã¡ã½ããã使ãã®ãåã¨ãããã¨ã®ããã§ãã
ã«ã¼ã
localStorageãã®ãã®ãã«ã¼ãã§åãã¦ãå
¨ä½ã«å¦çãããã¨ããå ´åã
ã»ã¨ãã©æ®éã®ãªãã¸ã§ã¯ãã®ããã«forã§åããã¨ãã§ãããã¨æã£ã¦ãããã§ããã
ã«ã¼ãã¯ä»¥ä¸ã®ãããªãã®ãèãããã¾ãã
// localStorage.key(i)ã§içªç®ã®ãã¼ã¿ã®keyãåããã for (var i=0; i<localStorage.length; i++){ var k = localStorage.key(i); localStorage.getItem(k); } //ã¤ã³ããã¯ã¹ã§keyãåã for (var i=0; i<localStorage.length; i++){ var k = localStorage[i]; localStorage.getItem(k); } //ã¤ãã¬ã¼ã·ã§ã³ for (var k in localStorage){ localStorage.getItem(k); } //jQueryã®eachã試ãã¦ã¿ãw $.each(localStorage,function(i,k){ localStorage.getItem(k); });
ã§ããããã¦è²ã
ãªãã¨ãåãã£ãã®ã§ãããããã¯ãã説æããã¾ãã«ãç´°ããã¦é¢åãªã®ã§ãããè¦ã¦èªåã§ç¢ºããã¦ãã ããw
ãã®è¾ºã®æåãQUnitã§ãã¹ããã¦ã¾ãã
å¦ç¿ãã¹ãçã«ãçµæ§é©å½ã«ç¢ºèªãããã¨æ¸ãã¦ãããã§ãããæ°ãã¤ãããçµæ§è¨ãä¸ããã¾ããã
ä¸å¿ä»¥ä¸ã«ãã¹ãã£ã³ã°ãããã§ãè²ã
ãªç°å¢ã§è¦ã¦ã¿ã¦ãã ããã
ãã¶ãä¸çªå¤§äºãªã®ã¯ä»¥ä¸ï¼ç¹ã
- FireFoxã¯ã¤ãã¬ã¼ã·ã§ã³ã§ããªãã
- Safariã¯key() ãç¡ãï¼
æå¾ã®ãã¤ãä¸çªããã£ãã®ã§ãããããã¾ã§ãèªåã®ç°å¢ã§ã¯ã
safariã®key()ã®æåããããããã©ããã¡ããã¨å®è£
ããã¦ããªãããã§ãã
ããããã³ã³ã½ã¼ã«ããã ã¨ããããfunctionã§ã¯ãªããlocalStorage.keyã¨ããã¨ã
"value"ã¨ããstringãæ»ãã¨ããè¬ä»æ§ãªåãããããã§ãããèªåã ãã§ããããï¼
ã㨠$.eachã¯ã¡ãã£ã¨ãããã°FireFoxã§ã¯ä½¿ããã(ã¤ãããªããã©)
ããããªãã®ã§ããã©ã¦ã¶ãã¨ã®å·®ãåããããã«ãã¹ããæ¸ãã¦ããããå ã«ãã®ä¸ã¤ã«éã£ã¦ã¯ãããªæãã§ã«ãã¼åºæ¥ã¾ãã
var db ={ set : function(key, obj){ localStorage.setItem(key, JSON.stringify(obj)); }, get : function(key){ return JSON.parse(localStorage.getItem(key)); }, each : function(fun){ try{ for (var i=0; i<localStorage.length; i++){ var k = localStorage.key(i); fun(k,db.get(k)); } }catch(e){ for (var key in localStorage){ if(key === 'key') continue; fun(key,db.get(key)); } } } };
ã¾ãããã®ã¾ã¾ç´ ç´ã«è¢«ããã ãã§ãããã¨ããããæå ã®ç°å¢ã§ã¯æ ¼ç´ã¨åå¾ã¨ã«ã¼ãã¯åºæ¥ã¾ãã
使ãæ¹ã¯ãã¹ããè¦ã¦ãã ããããã®ã©ããã¼ã®ãã¹ããè¼ãã¦ããã¾ãã
ã½ã¼ã¹ã¯ãã¡ãã
jxck / localStorage-test — Bitbucket
ã¤ãã³ãã¯ãã£ã¨è²ã ç½ ããããããªåãèããã®ã§ãããã©ããªãã§ãããã
w3cããååãä»æ§æ¸ããããããã®ãæä¾ãã¦ãããã¨ãèªåã¯åãããããã¦ãããããã§ããã©ãã
ã¾ããã¾ã ä»æ§ãçå®æ®µéã ããã¡ã³ãããã®å¤§å¤ã ãããã©ã
æ®å¿µãªãã¨ã«ä»æ¥ã¯ãã£ãããã ãã®ã©ããæ¸ãã¾ã§ã§çµãã£ã¦ãã¾ã£ãã®ã§ã次ã¯ããã使ã£ã¦ããä¸æ©å
ã«é²ã¿ããã¨ããã§ãã
ã¾ããã§ã使ããã°localStorageã¯ä¾¿å©ã ããéè¦ã«ãªã£ã¦è¡ãã¨æãã®ã§ãæ©ãä»æ§ã¨å®è£
ãå®å®ãã¦ãããã¨ããã§ããã
ä»åã®ä»¶ãããããªã¨ããçæã£ããæãã¦é ããã¨ããããã§ãã