如果声明仅在其他情况下正常工作

时间:2014-11-17 01:28:33

标签: javascript if-statement

这里的想法是创造一种“EULA墙”。出于某种原因,每当我测试这段代码时,如果我没有cookie,它运行得非常好,EULA弹出,接受按钮弹出,接受按钮给我cookie。但是,当我有cookie时,实际部分中没有任何内容发生。我目前有三个“命令”,一个会再次调出按钮(一个可视的调试),一个将文本传递给<p></p>,另一个调用运行open.window()的函数;功能:

</script>
<p id = "loc"></p>
    <script>
function printButton(){
            var btn = document.createElement("BUTTON");
            var text = document.createTextNode("accept");
            btn.appendChild(text);
            btn.setAttribute("onclick", "setCookie('accept','yes',365)")
            document.getElementById("loc").appendChild(btn);
        }
    </script>
    <script>
        function setCookie(cname, cvalue, exdays) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toUTCString();
            document.cookie = cname + "=" + cvalue + "; " + expires;
    location.reload();
        }
    </script>
    <script>
        function getCookie(cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1);
                if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
            }
            return "";
        }
    </script>
<p id = "testingstuff"></p>
    <script>
        function checkCookie() {
            var accept = getCookie("accept");
            if (accept != "") {
             newWindow();
             printButton();
             document.getElementById("testingstuff").innerHTML = "testing";
            } else {
    displayEULA();
    printButton();

            }
        }
        checkCookie();

    </script>
    <script>
function newWindow(){
window.open("http://google.com","_self");
}
    </script>
</body>

</html>

奇怪的是,这种事情会发生,但也许我只是遗漏了一些明显的东西。

0 个答案:

没有答案