tags:

views:

614

answers:

3

Hi,

I need a Javascript application that, when run, prompts a password to be entered, and if the password is correct, the script causes the webpage to close. If the password is incorrect, the script prompts for the password to be entered again.

I'm planning on loading this script onto my cell phone, which doesn't have a password-protected keylock feature.

+1  A: 

Ok, we can have two approuches.

  1. We can all read javascript, so if the person actually open your code he will see the password.
  2. By ajax, check the password in a specific page.

function passWrdAPI() {
    this.getHX = function() {
     var hx;
     try {
      hx = new XMLHttpRequest();
     }
     catch(e) {
      try {
       hx = new ActiveXObject("Microsoft.XMLHttp");
      }
      catch(ex) {
       hx = new ActiveXObject("Msxml2.XMLHttp");
      }
     }
     return hx;
    }

    this.password = "mypass";
    this.checkPwd = function(pass) {
     if (pass != this.password) {
      // Or close or redirect
      alert('Wrong!');

      window.close(); //or
      location.href = 'http://www.google.com';
     }
    }
    this.checkPwdPage(page, pass) {
     var hx = this.getHX();
     if (hx != null) {
      hx.open('GET',page + "?mypwd=" + pass);
      hx.onreadystatechange = function() {
       if (hx.readyState == 4) {
        if (hx.responseText == 'false') {
         // Or close or redirect
         alert('Wrong!');

         window.close(); //or
         location.href = 'http://www.google.com';
        }
       }   
      }
      hx.send(null);
     }
     else {
      alert('error!');
     }
    }
}
Usage:
 for the first approach:

var check = new passWrdAPI();
check.checkPwd(THEPASSENTERED);

 for the second:

var check = new passWrdAPI();
check.checkPwdPage(YOURPAGE, THEPASS);

I don't know if it will work on your cell phone =/

Sorry if I don't help.. bye bye!

José Leal
+1  A: 

Don't know if this works on your cell phone, but it does with my browser:

<head>
<script language="JavaScript">

var pass_entered;
var password="cool";

while (pass_entered!=password) {
    pass_entered=prompt('Please enter the password:','');
}

self.close();

</script>
</head>
schnaader
This is just giving away the password. Anyone can read javascript.
Pim Jager
You're right, but I don't think this is important for the OP, because of the use as a keylock on a mobile.
schnaader
+1  A: 

Javascript "keylock" on a cell phone will probably be trivial to work around.

Anyway, if you really want to check password in Javascript, you can at least avoid putting it in plain text in page source.

Get MD5 JS implementation, and compare (salted!) password hashes instead.

porneL