Internet Explorer上的本地存储无法工作

localstorage on Internet Explorer not working

本文关键字:存储 工作 Explorer Internet      更新时间:2023-09-26

localstorage似乎可以用于:-谷歌Chrome-Mozilla Firefox-Opera-迷你歌剧-可能是Safari

但不是在互联网浏览器上(我使用的是互联网浏览器11(。我的是7号窗户。我需要做同样工作的同等产品。这是一个项目,我在C:驱动器上做所有事情(安全性不重要(,所以我的协议是file:''。我做了一些研究,有些人通过添加来修复它

    <!DOCTYPE html>

但它对我不起作用。

这是我的代码:

    <!DOCTYPE html>
    <html>
        <head>
            <title>Login</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style type="text/css">
                * {
                    font-family:Cambria;
                    color:blue;
                }
            </style>
        </head>
        <body>
            <script>
                function transfer() {
                    confirm("Would you like to save your password for this site?");
                    var contents = document.getElementById('email_input').value;
                    var contents_2 = document.getElementById("password_input").value;
                    localStorage.setItem('user', contents);
                    localStorage.setItem('password', contents_2);
                    window.location.href = 'page2.html';
                    };
                var button_clicked = function(){
                    email_content = document.getElementById("email_input").value;
                    pass_content = document.getElementById("password_input").value;
                    points = 0;
                    if (email_content.length < 1){
                        document.getElementById("empty_1").innerHTML = ("*please input your email address");
                    } else {
                        document.getElementById("empty_1").innerHTML = ("<br>");
                        points += 1;
                    };
                    if (pass_content.length < 1){
                        document.getElementById("empty_2").innerHTML = ("*please input your password");
                    } else {
                        document.getElementById("empty_2").innerHTML = ("<br>");
                        points += 1;
                    };
                    if (points === 2){
                        transfer();
                    }
                };
            </script>
            <div id="top_bar" style="height:100px;background-color:lightslategray;">
                <marquee scrollamount="20" behavior="scroll"><p style="font-size:30px;color:white;">
                    Welcome, please login to your account to continue</p>
                </marquee>
            </div>
            <div>
                <div style="margin-left:500px;width:300px;height:200px;background-color:lightblue;"></div>    
                <div style="margin-left:440px;">
                    <div style="background-color:whitesmoke;width:350px;height:270px;margin-left:30px;border-radius:15px;
                         margin-bottom:30px;">
                            <div style="margin-left:40px;">
                                <h1>Login below</h1>
                                <p id="empty_1" style="color:red;"><br></p>
                                <p>Email address: <input id="email_input" type="text" style="width:150px;"/></p>
                                <p id="empty_2" style="color:red;"><br></p>
                                <p>Password: <input id="password_input" type="password" style="width:180px;"/></p>
                                <br>
                                <button onclick="button_clicked()">Submit</button>
                            </div>
                        </div>
                </div>
                <div style="margin-left:500px;width:300px;height:500px;background-color:lightblue;"></div>
            </div>
        </body>
    </html>

另存为page1.html第二页是:

    <!DOCTYPE html>
    <html>
        <head>
            <title id='title'>title goes here</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style type='text/css'>
                h1 {
                    color:blue;
                }
            </style>
        </head>
        <body>
            <h1 id='my_title'>Title</h1>
            <h2 id='my_pass'>Title</h2>
            <script>
                var full_name = localStorage.getItem('user');
                list = [];
                for (i=0;i<full_name.length;i++){
                    if (full_name[i]==="@"){
                        break;
                    }
                    else{
                        list.push(full_name[i]);
                    }
                };
                document.getElementById("my_title").innerHTML = ("Name: " + list.join(""));
                var full_pass = localStorage.getItem('password');
                document.getElementById("my_pass").innerHTML = ("Email address: " + full_name);
            </script>
        </body>
    </html> 

另存为page2.html

感谢所有的回答。

添加doctype声明意味着浏览器会以应有的方式(即HTML5(解析标记。

Internet Explorer在本地存储方面存在一些问题。首先,它在8之前的版本中根本不起作用——你没有在帖子中指定你正在运行的版本。

重要:您提到您在C:驱动器上运行:这是否意味着您使用的是file://协议而不是http?如果是这样,问题就解决了。使用文件协议会导致各种问题,尤其是localStorage在IE中根本不起作用。

如果您仍然有问题,可能会发现您需要修改浏览器的安全设置以允许本地存储。

此页面包括一个矩阵,详细说明了各种浏览器中的localStorage支持:

http://www.html5rocks.com/en/features/storage

请务必查看Mark Pilgrim出色的HTML5资源,其中包括一些用于检测storage事件的IE特定代码:

http://diveintohtml5.info/storage.html

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

我试过IE 11,对我有用!