2017-02-11 22 views
0

なぜ私の関数が「ブラケット」の「ライブデモンストレーション」でしか動作しないのか分かりませんが、私がindex.htmlファイルで開こうとしたときは理解できません...機能のプロンプト(Javascript)が機能しないのはなぜですか?

パスワード保護領域:

function passWord() { 
var testV = 1; 
var pass1 = prompt('Please Enter Your Password',' '); 
    while (testV < 3) { 
    if (!pass1) 
    history.go(0); 
    if (pass1.toLowerCase() == "letmein") { 
    alert('You Got it Right!'); 
    window.open('/html/ok.html',"_self"); 
    break; 
    } 
    testV+=1; 
    var pass1 = 
    prompt('Access Denied - Password Incorrect, Please Try  Again.','Password'); 
} 
if (pass1.toLowerCase()!="password" & testV ==3) 
history.go(0); 
return " "; 
} 

おかげで、すべて、私を助けてください:素敵な機能ですD

+1

devツールでパスワード(および「OK」URL)を見つけるために皆さんがJSコードをチェックアウトすることができます。それは絶対にしないでください。そしてこれとは別に、「大括弧」はどういう意味ですか? – Lucero

+0

@Lucero私の無実は、彼が単にjsを学ぼうとしていると思うのですが、これを決してプロダクションで使うことはないでしょう:D – PierreDuc

+0

@PierreDucおそらく、意識を高めることは間違いではないでしょう。 http://thedailywtf.com/series/code-sod – Lucero

答えて

0

...とにかく、あなたが実際にあなたのindex.html内の関数を呼び出していますか?

<html> 
<head> 
    <script> 
     function passWord() { 
     //... (place your code here) 
     } 
    </script> 
</head> 
<body onload="passWord()"></body> <!--here you actually call the function--> 
</html> 

これはほんの一例で、html内でイベント発信者を使用することに戸惑うことがあります。また、あなたのスクリプトタグ内の関数呼び出しを追加することができます。

<script> 
    function passWord() { 
     //... (place your code here) 
    } 
    passWord(); //now the function actually gets executed 
</script> 
+0

それは私のindex.html私は何を変更する必要がありますか? <入力タイプ= "ボタン"値= "保護されたエリアを入力" onClick = "passWord()"> AlfredoFalke

+0

私はmein.jsというファイルを関数と私のindex.html – AlfredoFalke

0

あなたがtestVの値をリセットhistory.go(0);でページを更新しているため。

あなたは何が起こっているのを見ることができるように私はいくつかのコンソールログを追加しました:

function passWord() { 
    var testV = 1; 
    var pass1 = prompt('Please Enter Your Password'); 
    while (testV < 3) { 
    if (pass1.toLowerCase() == "letmein") { 
     alert('You Got it Right!'); 
     window.open('/html/ok.html', "_self"); 
     break; 
    } 
    testV++; 
    var pass1 = prompt('Access Denied - Password Incorrect, Please Try Again.'); 
    console.log(testV); 
    } 
    if (pass1.toLowerCase() != "password" & testV == 3) 
    { 
    console.log("Refresh"); 
    history.go(0); 
    } 
    return " "; 
} 

passWord(); 

フィドル:https://jsfiddle.net/s2ejwk9p/

history.go(0);を取り出し、あなたは大丈夫でしょう。

+0

それは私のindex.html私は何を変更する必要がありますか? – AlfredoFalke

+0

私はmein.jsというファイルを持っています。関数と私のindex.html – AlfredoFalke

関連する問題