Created
November 26, 2012 04:45
-
-
Save uupaa/4146646 to your computer and use it in GitHub Desktop.
localStorage with ByteArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html><html><head><meta charset="utf-8"> | |
<script> | |
window.onload = function() { | |
// localStorage に対するバイナリ文字列のデータ読み書きテスト | |
// MobileSafari, AndroidBrowserでは、電源OFF -> ON でデータが消失する | |
if (0) { | |
// null(0x00) を含まない文字列データ | |
if (!localStorage.getItem("dummy")) { | |
localStorage.setItem("dummy", "dummy"); | |
alert("write data"); | |
location.reload(true); // force reload | |
return; | |
} | |
alert(localStorage.getItem("dummy")); | |
return; | |
} | |
if (1) { | |
// null(0x00) を含むバイナリ文字列データ | |
if (!localStorage.getItem("test")) { | |
// key "test" から値が取得できない場合は、 | |
// localStorageに書き込み、ページを強制リロードします | |
var byteString = | |
String.fromCharCode(0x00,0x01,0x32,0x33, // "\0\123" | |
0x00,0x01,0x32,0x3042); // "\0\12あ" | |
localStorage.setItem("test", byteString); | |
alert("write data"); // 再度ここを通る場合はデータが壊れている | |
location.reload(true); // force reload | |
return; | |
} | |
// key "test" が存在する場合は取得し、値の妥当性をチェックします | |
var resultByteString = localStorage.getItem("test"); | |
alert("result: " + resultByteString); | |
var ready = true; | |
if (resultByteString.charCodeAt(0) !== 0x00 || | |
resultByteString.charCodeAt(1) !== 0x01 || | |
resultByteString.charCodeAt(2) !== 0x32 || | |
resultByteString.charCodeAt(3) !== 0x33 || | |
resultByteString.charCodeAt(4) !== 0x00 || | |
resultByteString.charCodeAt(5) !== 0x01 || | |
resultByteString.charCodeAt(6) !== 0x32 || | |
resultByteString.charCodeAt(7) !== 0x3042){ | |
ready = false; | |
} | |
alert("localStorage with ByteString: " + (ready ? "READY" | |
: "NOT READY")); | |
if (ready) { | |
alert(resultByteString); | |
} | |
} | |
}; | |
</script> | |
</head><body> | |
</body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ByteArray といいつつ、バイナリ文字列ですけども