Кук ie не сохраняет цвет фона из-за CSS - PullRequest
0 голосов
/ 18 марта 2020

Когда я не связываю свой внешний CSS с моим HTML файлом, я могу изменить цвет фона. Однако, когда я добавляю свой внешний CSS, цвет фона копирует CSS, а не значение, введенное в приглашении. Что я делаю неправильно?

Вот мой html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Cookie</title>
<link rel="stylesheet" href="mystylesheet.css">
<script type="text/javascript">
<!-- 
    var myColor = prompt("Please enter a hexadecimal color code (must be 6 characters, including 0 thru 9 and A thru F):","FFFFFF");
    var myName = prompt("Please enter your name:","Cookie Monster");

    if ((myColor == null) || (myColor == "") || (myColor.length < 6 || myColor.length > 6)) myColor = "#ffffff";

     var keepCookie = (confirm("A cookie will be set. Delete cookie?")) ? "delete" : "keep";

    if (keepCookie == "delete") {
        document.cookie="testCookie=" +"YColor=" + myColor + " " + "name=" + myName + ";expires=20-May-2035";
        message= "Here is the cookie that was deleted:  ";
        message += document.cookie;
        alert(message);
    } 
    else {
        document.cookie="testCookie=" + "YColor=" + myColor + " " + "name=" + myName + ";expires=20-May-2035"; // the expiry date must be somewhere in the future, or the cookie will not be set in Firefox or Chrome
        message= "Here is the cookie that was set on your system:  ";
        message += document.cookie;
        alert(message);
    }


    var start = document.cookie.indexOf("YColor=");
    start=start + 7;
    var pos = "#" + document.cookie.substr(start,6);
    alert("Your background color will be: " + pos);

    var nameLengthS = document.cookie.indexOf("name=");
    nameLengthS=nameLengthS + 5;
    var PersonName = document.cookie.substr(nameLengthS,25);
    alert("Your name is: " + PersonName);


//-->
</script>
</head>

<body>
<h3>Cookie</h3>
<hr />
<script type="text/javascript">
<!-- 

    if ((pos == null) || (pos == "")) pos = "#ffffff";
    document.bgColor = pos;
    document.write("Your background color is: " + document.bgColor + "<br />");
    document.write("Your name is: " + window.PersonName + "<br />");
    document.write("Cookie contents: " + document.cookie);
    document.write();

//-->
</script>
</body>
</html>

1 Ответ

0 голосов
/ 18 марта 2020

Это не сохранение, потому что вы устанавливаете цвет перед css загрузкой. Например,

document.bgColor = pos;

загружается перед установкой CSS цвет Обходной путь - установить цвет после загрузки CSS. Ниже приведен код, который использует load listener для изменения цвета фона нашего выбора.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Cookie</title>
        <link rel="stylesheet" href="mystylesheet.css">
        <script type="text/javascript">
            var myColor = prompt("Please enter a hexadecimal color code (must be 6 characters, including 0 thru 9 and A thru F):","FFFFFF");
            var myName = prompt("Please enter your name:","Cookie Monster");

            if ((myColor == null) || (myColor == "") || (myColor.length < 6 || myColor.length > 6)) myColor = "#ffffff";

             var keepCookie = (confirm("A cookie will be set. Delete cookie?")) ? "delete" : "keep";

            if (keepCookie == "delete") {
                document.cookie="testCookie=" +"YColor=" + myColor + " " + "name=" + myName + ";expires=20-May-2035";
                message= "Here is the cookie that was deleted:  ";
                message += document.cookie;
                alert(message);
            } 
            else {
                document.cookie="testCookie=" + "YColor=" + myColor + " " + "name=" + myName + ";expires=20-May-2035"; // the expiry date must be somewhere in the future, or the cookie will not be set in Firefox or Chrome
                message= "Here is the cookie that was set on your system:  ";
                message += document.cookie;
                alert(message);
            }


            var start = document.cookie.indexOf("YColor=");
            start=start + 7;
            var pos = "#" + document.cookie.substr(start,6);
            alert("Your background color will be: " + pos);

            var nameLengthS = document.cookie.indexOf("name=");
            nameLengthS=nameLengthS + 5;
            var PersonName = document.cookie.substr(nameLengthS,25);
            alert("Your name is: " + PersonName);


        </script>
    </head>
    <body>
        <h3>Cookie</h3>
        <hr />
        <script type="text/javascript">
            function changeBackground() 
            {
                if ((pos == null) || (pos == "")) pos = "#ffffff";
                document.bgColor = pos;
                document.write("Your background color is: " + document.bgColor + "<br />");
                document.write("Your name is: " + window.PersonName + "<br />");
                document.write("Cookie contents: " + document.cookie);
                document.write();
            }

            window.addEventListener("load",function() { changeBackground() });
        </script>
    </body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...