【问题标题】:Why does my function prompt(Javascript) doesn't work?为什么我的功能提示(Javascript)不起作用?
【发布时间】:2017-02-11 10:56:45
【问题描述】:

我不明白为什么我的函数只在“括号”的“现场演示”中有效,而当我尝试用我的 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

【问题讨论】:

  • 您知道每个人都可以查看您的 JS 代码以在开发工具中找到密码(以及“ok” URL)吗?不要那样做,永远不要。除此之外,“括号”是什么意思?
  • @Lucero 我的天真让我觉得他只是想学习 js,永远不会在生产中使用它:D
  • @PierreDuc 也许,但提高认识肯定没有错。太多这样的代码最终会公开......thedailywtf.com/series/code-sod
  • 仅用于学校项目 xD

标签: javascript html prompt adobe-brackets


【解决方案1】:

因为您正在使用 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);,你会没事的。

【讨论】:

  • 那是我的 index.html 我应该修改什么?
  • 我有一个名为 mein.js 的文件,其中包含函数和我的 index.html
【解决方案2】:

这是一个不错的函数。无论如何,您实际上是在 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>

【讨论】:

  • 那是我的 index.html 我应该修改什么?
  • 我有一个名为 mein.js 的文件,其中包含函数和我的 index.html
猜你喜欢
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
相关资源
最近更新 更多