-1

Cookie のように確認する必要があるサイトがあります。しかし、モバイルのコンセプトなので、ローカル ストレージを使用したいと考えています。テストを通じてアイテムが設定されていることは知っていますが、そうでない場合、ページをリダイレクトできません。

<script language="javascript">
 if (localStorage.getItem('itemVerified') = 'True') {
window.location = "success.html";
}
else {
alert("You are not able to enter this site!");
window.location = "error.html";
}
</script>
4

1 に答える 1

0

2 つのエラーが発生する可能性があります。最初のエラーは、比較文字列に = 記号がないことです。

正しくない:if (localStorage.getItem('itemVerified') = 'True') {

正しい:if (localStorage.getItem('itemVerified') == 'True') {

2 番目のケースは、ドキュメントの読み込み元のサイトを確認する場合に、イベント リスナーにアクションを追加する必要がある場合です。次の手順を実行できます。

    <script type="text/javascript">
     var verifyCookie = function(){

   if (localStorage.getItem('itemVerified') == 'True') {
    console.log("All right");
        window.location = "success.html";
   }
   else {
    alert("You are not able to enter this site!");
    window.location = "error.html";
   }
      };
     window.addEventListener ('DOMContentLoaded', verifyCookie, false);
    </script>

よろしく。

于 2013-04-14T08:47:37.720 に答える