1

localStorage APIを使用して保存されたアイテムの値は、ユーザーが表示できますか?クッキーのように?

4

2 に答える 2

1

SafariとChromeのWebインスペクターでは、そのデータを検査できます。他のブラウザがどのように処理するかはわかりませんが、編集されていないことに依存できるものではありません。

于 2010-10-20T15:22:30.177 に答える
0

ユーザーに表示するアイテムを簡単にロードできます。

ページの読み込み時に Javascript を使用して、ローカル ストレージ内のすべてのアイテムをループするだけです...

<body>
<script language="JavaScript" type="text/javascript">
var item = "";//array to hold string values for each key value
var key ="";//array to hold string values for each key name
for (i=0;i < localStorage.length;i++) {

    var count = 0;
    if (key != "" | key != null) { //or matches what you're looking for
      item[count] = localStorage.getItem(key);
      key(i) = localStorage.key(i);
      count += 1;
    }

 }

function load_table()
{
if (item == "" || item == " " || item == null) {
document.write("<div id=\"list_table\" style=\"display: block;\">");
document.write("<h3>You have no stored items.</h3>");
document.write("</div>");
}
else {
     document.write("<div id=\"list_table\" style=\"display: block;\">");
     document.write("<h3>Stored Items</h3>");
     document.write("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");

     for (i=0;i < item.length;i++) {
          document.write("<tr><td width=\"33%\" valign=\"top\">"+item(i)+"</td>");
          document.write("<td width=\"67%\" valign=\"top\" style=\"padding-left: 0px; text-transform: capitalize;\">"+key(i)+"</td></tr>");
          }
 }


document.write("</table>");
document.write("</div> <!-- end list_table div -->");
} // end if (item != "")
} // end load_table
</script>

html では、表示または非表示にできる div を適切な場所に貼り付けます。

<div id="items_table" style="display: none;">
<script language="JavaScript" type="text/javascript">
//alert("calling load_table");
load_table();
//alert("DONE calling load_table");
</script>
</div>

ユーザーがリンクをクリックすると、ページの読み込み中に入力され、非表示になっている items_table を表示できます。何千ものアイテムをロードする必要がない限り、すぐにロードされます。

必要に応じて、スタイル表示プロパティの表示をブロックとなしの間で切り替えるリンクを掘り下げることができます。

于 2011-11-25T05:22:36.337 に答える