m2

data スキームで localStorage できるのか試してみた。

コードはこんな感じ。

javascript:(function(){
    var ifr = document.createElement('iframe');
    ifr.src = 'data:text/html;charset=utf-8,%3Chtml%3E%0D%0A%3Cbody%3E%0D%0A%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0D%0Awindow.addEventListener(%22message%22%2C%20receiveMessage%2C%20false)%3B%0D%0Afunction%20receiveMessage(e)%20%7B%0D%0A%20%20%20%20alert(localStorage%5B%22aaa%22%5D)%3B%0D%0A%20%20%20%20localStorage%5B%22aaa%22%5D%20%3D%20e.data%3B%0D%0A%20%20%20%20alert(localStorage%5B%22aaa%22%5D)%3B%0D%0A%7D%0D%0A%3C%2Fscript%3E%0D%0A%3C%2Fbody%3E%0D%0A%3C%2Fhtml%3E%0D%0A';
    document.body.appendChild(ifr);
    ifr.addEventListener('load', function() {
        alert('post');
        ifr.contentWindow.postMessage('aaaaaaaaaa', '*');
    }, false);
})()

「data:text/html〜」中の HTML 部分は以下のようになっています。

<html>
<body>
<script type="text/javascript">
window.addEventListener("message", receiveMessage, false);
function receiveMessage(e) {
    alert(localStorage["aaa"]);
    localStorage["aaa"] = e.data;
    alert(localStorage["aaa"]);
}
</script>
</body>

http://gist.github.com/419354

<結果>

Opera10.5 Chrome5.0.375.55 Firefox3.6.3
保存はできるが別の iframe から読み出せない
すみません、保存できました。Firefox と同じ挙動です。
「Uncaught Error: SECURITY_ERR: DOM Exception 18」 使える。ただし data スキーム全体で1ドメインのような挙動

<参考>