admin管理员组

文章数量:1292121

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.

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.

Share Improve this question asked Nov 13, 2008 at 12:44 davedave
Add a ment  | 

3 Answers 3

Reset to default 3

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>

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 pare (salted!) password hashes instead.

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.';
        }
    }
    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.';
                    }
                }           
            }
            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!

本文标签: Simple Password Javascript CodeStack Overflow