簡體   English   中英

為什么我的函數提示(Javascript)不起作用?

[英]Why does my function prompt(Javascript) doesn't work?

我不明白為什么我的函數只能在“ Brackets”的“實時演示”中起作用,而當我嘗試使用index.html文件打開它時卻不起作用...

這就是我的密碼保護區域功能:

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

那是一個很好的函數。無論如何,您實際上是在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>

因為您正在使用history.go(0);刷新頁面history.go(0); 重置testV的值。

我添加了一些控制台日志,以便您可以查看發生了什么:

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); 一切都會好起來的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM