【问题标题】:localstorage on Internet Explorer not workingInternet Explorer 上的本地存储不起作用
【发布时间】:2014-06-16 23:11:48
【问题描述】:

localstorage 似乎适用于: - 谷歌浏览器 - 火狐浏览器 - 歌剧 - 歌剧迷你 - 可能是 Safari

但不在 Internet Explorer 上(我使用的是 Internet Explorer 11)。我的是windows 7。 我需要能做同样工作的等价物。这是一个项目,我在我的 C: 驱动器上做所有事情(安全性并不重要)所以我的协议是文件:\。我做了一些研究,有些人通过添加来解决它:

    <!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

感谢所有答案。

【问题讨论】:

  • IE的版本是多少?
  • 你用的是什么操作系统?
  • 如果您使用的是 IE 11,请确保将其修补到最新版本(Windows 更新),因为在 Win7 SP1 的初始版本中存在错误,并且本地存储无法按预期工作。 stackoverflow.com/a/21156133/3535297
  • @Wolfdog 检查您的 IE 版本 单击浏览器顶部的菜单,直到您看到“关于 Internet Explorer”的内容,我无法告诉您确切的位置,因为我不知道是什么您正在使用的版本
  • 另外,您可能想考虑除&lt;marquee&gt; 之外的另一个选项,它是非标准的,不建议按照MDN 使用

标签: javascript html internet-explorer local-storage


【解决方案1】:
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

我尝试使用 IE 11,它对我有用!

【讨论】:

    【解决方案2】:

    添加 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

    【讨论】:

    • 对于使用文件协议打开的文档,localStorage 是否为undefined,在这种情况下,document.domain 等于空字符串?
    • 您提到localStorage 在 8 之前不能工作。sessionStorage 在 9 之前的 IE 版本中也不能工作吗?
    • 从 IE11 开始,可以在 file: 上使用 localStorage - 请参阅localStorage object is undefined in IE 的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2013-08-10
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多