2013-04-10 4 views
0

Я недавно пытался создать простую систему учетных записей на своем веб-сайте. Если у вас есть учетная запись, игра появится, и если вы не скажете эту страницу, You need an account!Скрытие HTML до утверждения if

Я не могу понять, как скрыть код, встроенный в HTML, до моего оператора if.

Это мой код до сих пор:

<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> 
+1

У вас есть дисплей в обратном направлении, то есть, вы скрываете элемент, если матч паролей. 'display' of' "none" 'означает" не отображать ". (Но даже если вы его заработаете, он фактически не защитит вашу страницу от пользователей с помощью нескольких ноу-хау в Интернете.) – nnnnnn

+0

Ваш html не является корректным (без закрывающего тега 'center'). это может привести к отказу решения нимчимского. – collapsar

ответ

0

Увидев ваш сайт, ясно, что вы планируете использовать несколько паролей. Затем я рекомендовал бы использовать массив для хранения паролей, а затем проверить, включен ли вход в массив.

Working Demo

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; 
} 
1
document.getElementById('Game').style.display = "block" 

Пароль будет просматриваться любым с браузером, хороший пароль, хотя.

diplsay:block - его видимый display:none не видно.

+0

Игра по-прежнему не скрыта, когда я ввел неправильный пароль или вообще не пароль. Могу ли я скрыть make s скрытый в инструкции if? – 2013-04-10 11:51:55

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!") 
0

второстепенной adjustmenst к ним чимпскому решению/Arun р а Джони (в 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> 
Смежные вопросы