繁体   English   中英

Internet Explorer上的localstorage无法正常工作

[英]localstorage on Internet Explorer not working

localstorage似乎可以工作: - 谷歌浏览器 - Mozilla Firefox - Opera - Opera mini - 可能是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

所有答案都赞赏。

添加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,这对我有用!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM