2017-02-11 67 views
0

我不明白为什么我的功能只适用于“Brackets”的“现场演示”,但不是当我尝试用我的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

你知道每个人都可以检查出你的JS代码来找到开发工具中的密码(和“ok”URL)吗?不要那样做,永远不要这样做。除此之外,“括号”是什么意思? – 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我应该修改什么? AlfredoFalke

+0

我有一个名为mein.js的文件,函数和我的index.html – AlfredoFalke

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);,你会没事的。

+0

这就是我的index.html我应该修改什么? <脚本类型= “/ JS/mein.js”> – AlfredoFalke

+0

我有一个文件名为mein.js与函数和我的index.html – AlfredoFalke