本地存储不保存

时间:2013-11-26 21:23:42

标签: javascript jquery local-storage

我正在使用firefox beta。我做了一个测试,我的浏览器说它支持本地存储。我的代码只是提醒我存储的变量,但每次关闭浏览器并重新打开它时,存储的数字为空,未定义或只是空白。我在线测试所以它应该工作。我测试了铬,它似乎工作正常。可能有什么原因导致它无法在Firefox或其他浏览器上运行?

<!DOCTYPE html>
<html lang=en>
    <head>  
        <title>local Storage test</title>
    </head>
    <body>
        <p class="save"> save score</p>
        <p class="alert"> alert score</p>
        <script src="JS/jquery-1.6.min.js"></script>
        <script>
            localStorage.theScore;
            lastScore="";
            lastScore2="";
            //test if local storage is supported
            if(window.localStorage!==undefined){
                alert('Yes!');
            }else{
                alert('Your browser is outdated!');
            }

            //array to hold score
            var scoreArray={
                score : 500 
            }

            //if user clicks alert 
            $(".alert").click(function(){

                //im trying two ways to here but neither seem to work
                lastScore=localStorage.theScore;    
                lastScore2=localStorage.getItem('theScore');

                alert(lastScore);    
                alert(lastScore2);   
            });

            //when user clicks save it should save variable score in local storage
            $(".save").click(function(){
                localStorage.setItem('theScore', scoreArray.score);

            });

        </script>

    </body>
</html>

0 个答案:

没有答案