小さな情報を保存するために Cookie 以外にどのような方法を使用できますか?
iframe を使用して特定のコンテンツをロードするウィジェットを構築しています。その iframe 内でアプリケーションのさまざまな状態を保存したいのですが、ウィジェットが iframe 内のコンテンツと同じドメインにない場合 (99% の時間)、問題が発生します。
何か案は?
ああ、これは jQuery です。
なぜみんなが代わりにHTML5ローカルストレージを使うように言っているのか分かりません。あなたの問題はセキュリティの1つです。
コミュニティウィキのリンクについては、上記の私のコメントを参照してください。https://www.owasp.org/index.php/HTML5_Security_Cheat_Sheetおよびhttp://dev.w3.org/html5/webstorage/#security-localStorageも参照してください。
Cookies are the way to go if you want to save persistent data for the specific user, however you can use HTML5, the difference being that the data stored with HTML is not explicitly shared with the server unless you use HTML5's sessionStorage
. (session storage uses the same API but stores data specifically against the user session and is not persistent beyond session close);
Saving state with the HTML5 API is actually very easy, it is also event driven meaning that an event is fired whenever data is modified allowing you to listen for changes and act accordingly within your javascript / jQuery.
localStorage.name = 'David';
would create a variable within the localStorage called name, with the value David. Close your browser, re-open and console.log the variable, you will notice it persists.
Be wary that localStorage, despite previous documentation will only store strings of data and objects must be serialised and de-serialised if you want to store / retrieve them.
The actual spec is much larger and you can read more about it here:
Tips and tricks on using HTML5 storage
If you are not looking for persistant data you can use jQuery's .data()
methods to store arbitrary data against a DOM nodefor later use. This is very useful, certainly if you are developing widgets and need to store non persistent state against a DOM node.
Additional:
ie7 and ie8 support is lacking but you can use a framework like modernizr.js to force legacy browser compatibility with HTML 5.
Jquery 以外に使用できる
1. 状態を保存するためのハッシュタグ
2. HTML5 History API
3. HTML5 ローカルストレージ