0

我最近一直在尝试在我的网站上设置一个简单的帐户系统。如果您有帐户,游戏会出现,如果没有,页面会显示,You need an account!

在我的 if 语句之前,我似乎无法弄清楚如何隐藏 HTML 嵌入代码。

到目前为止,这是我的代码:

<html>
    <body>
        <div id="Game">
            <center>
                <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="700" height="600" align="middle" id="kingdom-rush-1-082">
                    <param name="movie" value="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf">
                    <param name="quality" value="high">
                    <param name="AllowScriptAccess" value="always">
                    <!--[if !IE]>-->
                        <object type="application/x-shockwave-flash" data="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf" width="700" height="600">
                            <param name="movie" value="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf">
                            <param name="quality" value="high">
                            <param name="AllowScriptAccess" value="always">
                        <!--<![endif]-->
                        <!--[if !IE]>-->
                        </object>
                    <!--<![endif]-->
                </object>
        </div>
        <script type="text/javascript">
            var pass = window.prompt("Type Your Password")
             if ((pass == 'bacon'))
                 document.getElementById('Game').style.display = "none"
            else 
                 document.write("You need an account for this!")
        </script>
    </body>
</html>
4

4 回答 4

1
document.getElementById('Game').style.display = "block"

任何使用浏览器的人都可以查看密码,不过密码很好。

diplsay:block表示它可见 display:none它不可见。

于 2013-04-10T11:43:04.120 回答
1

首先使用样式隐藏html元素

<div id="Game" style="display: none">

然后在脚本中

var pass= window.prompt("Type Your Password")
if ( (pass=='bacon') )
    document.getElementById('Game').style.display = "block"
else
    document.write("You need an account for this!")
于 2013-04-10T11:45:02.977 回答
0

看过您的网站后,很明显您打算使用多个密码。然后我建议使用数组来存储密码,然后检查输入是否包含在数组中。

工作演示

HTML:

<div id="result">Waiting for password input...</div>
<div id="hiddenContent" style="display: none">This content is normally hidden unless a user has entered the correct password. Using Javascript is not a very good way to secure content though, as it can easily be bypassed.</div> 

Javascript:

var passwords = new Array();
passwords[0] = "pass1";
passwords[1] = "pass2";
passwords[2] = "pass3";
// This is your array of passwords. You can add as many as you want!

var passwordInput=prompt("Please enter your password...");
// This will ask for the user to enter their password

if (inArray(passwordInput, passwords)===true) {
    // This uses the inArray function to check if the users input is found within the array.
    document.getElementById("result").innerHTML="password found";
    document.getElementById('hiddenContent').style.display = "block"
    // If the password is found then it sets content of the <div> and makes it possible to view the hidden content.
}
else {
    document.getElementById("result").innerHTML="password not found";
    // If the password is not found then it sets the content of the <div>
}

// This function checks if a variable is found within an array
function inArray(variable, array) {
    var length = array.length;
    for(var i = 0; i < length; i++) {
        if(array[i] == variable) return true;
    }
    return false;
}
于 2013-04-14T02:45:59.983 回答
0

对 nimchimpsky / arun p johny 的解决方案进行微调(center元素正确终止)。如果您选择接受我的回答,您应该接受其中一位指定贡献者的回答。

<html>
    <body>
        <div id="Game" style="display:none;">
            <center>
                <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="700" height="600" align="middle" id="kingdom-rush-1-082">
                    <param name="movie" value="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf">
                    <param name="quality" value="high">
                    <param name="AllowScriptAccess" value="always">
                    <!--[if !IE]>-->
                        <object type="application/x-shockwave-flash" data="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf" width="700" height="600">
                            <param name="movie" value="http://m.toogame.com/k/swfs/kingdom-rush-1-082_80r.swf">
                            <param name="quality" value="high">
                            <param name="AllowScriptAccess" value="always">
                        <!--<![endif]-->
                        <!--[if !IE]>-->
                        </object>
                    <!--<![endif]-->
                </object>
            </center>
        </div>
        <script type="text/javascript">
            var pass = window.prompt("Type Your Password")
             if ((pass == 'bacon'))
                 document.getElementById('Game').style.display = "block"
            else 
                 document.write("You need an account for this!")
        </script>
    </body>
</html>
于 2013-04-10T12:17:15.603 回答