最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

HTMLJavascript - username and password - Stack Overflow

programmeradmin5浏览0评论

This html code below is suppose to execute and launch an alert asking for a username and if you get the username correct than it will ask for a password but nothing happens if i open this page in a browser. Why this doing this? What am I missing? Please don't tell me this is a unsecure way to do this I already know I'm just improvising until I implant a better way.

<html>
<head>
<script language="JavaScript">
var username;
var user1="grant"
username=prompt('Please Log in. Username:',' ');
if (username=user1);
   var pass1="password";
   password=prompt('If you are suppose to be here you have a password. Please type                  it now:',' ');
    if (password==pass1);
    else {
        window.location="wrongpassword.html";
    }
else {
  window.location="wrongpassword.html";
}
</script>
<body>
</body>
<html>

This html code below is suppose to execute and launch an alert asking for a username and if you get the username correct than it will ask for a password but nothing happens if i open this page in a browser. Why this doing this? What am I missing? Please don't tell me this is a unsecure way to do this I already know I'm just improvising until I implant a better way.

<html>
<head>
<script language="JavaScript">
var username;
var user1="grant"
username=prompt('Please Log in. Username:',' ');
if (username=user1);
   var pass1="password";
   password=prompt('If you are suppose to be here you have a password. Please type                  it now:',' ');
    if (password==pass1);
    else {
        window.location="wrongpassword.html";
    }
else {
  window.location="wrongpassword.html";
}
</script>
<body>
</body>
<html>
Share Improve this question asked Nov 2, 2013 at 16:04 applesapples 1472 gold badges6 silver badges16 bronze badges 3
  • if (username=user1); Really? ... single =, ; after if expression, and no curly braces? – XCS Commented Nov 2, 2013 at 16:07
  • Also the language attribute on the script tag is deprecated. Use type="text/javascript". – Ingo Bürk Commented Nov 2, 2013 at 16:11
  • Please clarify who you are referring to. – Ingo Bürk Commented Nov 2, 2013 at 16:16
Add a ment  | 

2 Answers 2

Reset to default 2

You are missing some braces. Corrected code (fiddle: http://jsfiddle/RyjhP/1/):

var user1="grant";
var username=prompt('Please Log in. Username:',' ');
if (username==user1){
   var pass1="password";
   password=prompt('If you are suppose to be here you have a password. Please type                  it now:',' ');
    if (password==pass1){
        alert("correct!")
     }
    else {
        window.location="wrongpassword.html";
    }
}
else {
  window.location="wrongpassword.html";
}

short way to do that.

js

window.onload=function(){
window.location=prompt('Enter Pass')!='password'?'wrong.html':'ok.html'
}

example

http://jsfiddle/6KzDJ/

but this is a very bad thing.

you should check the password from a secure location with ajax.

发布评论

评论列表(0)

  1. 暂无评论