Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Password Script

  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Location
    Spokane, WA
    Posts
    512
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Password Script

    I am using a javascript password system to keep out Average Joe from a certain page. This is the script:

    <script type="text/javascript"><!--

    var n=3;

    var pass=prompt("What is the password, you have "+n+" chance(s) left.","");n--;

    while(pass.toLowerCase()!="password" && n>0){pass=prompt("What is the password, you have "+n+" chance(s) left.","");n--}

    if(pass.toLowerCase()!="password"){location.replace("http://www.w3.org/");}

    //--></script>

    If this script is placed on a page, a dialoge box pops up and prompts the user for a password in order to enter the page. This box comes with an "OK" and a "Cancel" button. The thing works great, except it just lets you in if you push the cancel button. Does anyone know how I can get the cancel button to redirect the user to a different page or any other way to fix this? Thanks.

    -Ben
    Thoughts determine what you want,
    Actions determine what you get.

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Put this after the prompt:
    Code:
    if (!pass && typeof pass=="object") {
        alert("you pressed cancel");
    }
    /Daniel

  • #3
    Regular Coder
    Join Date
    Jun 2003
    Location
    Spokane, WA
    Posts
    512
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, its an improvement... but the only difference is know that it alerts the user that he/she has clicked cancel, and then lets them into the page anywho. Is there something to add to that so that if the cancel button is clicked, it redirects them to another site?

    Thanks!

    -Ben
    Thoughts determine what you want,
    Actions determine what you get.

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes:

    Code:
    if (!pass && typeof pass=="object") {
        pass = "wrongPassword";
    }
    Or:
    Code:
    if (!pass && typeof pass=="object") {
        location = "anotherpage.html";
    }
    /Daniel

  • #5
    Regular Coder
    Join Date
    Jun 2003
    Location
    Spokane, WA
    Posts
    512
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That worked great! I had to take out the multiple-tries part to get it to work correctly, but it works great! Thanks!

    -Ben
    Thoughts determine what you want,
    Actions determine what you get.

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There was just a huge thread on password protection using JavaScript a few days ago. You might want to consider checking it out:

    need help with password protecting my website
    Moderator @ WebDeveloper.com
    Mentor @ WebXpertz.net

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Ben@WEBProp
    That worked great! I had to take out the multiple-tries part to get it to work correctly, but it works great! Thanks!

    -Ben
    No problem if you want to use multiple tries:
    Code:
    var n=3;
    var pass="wrongPassword";
    while(pass.toLowerCase()!="password" && n>0){
        pass=prompt("What is the password, you have "+n+" chance(s) left.","");
        n--;
        if (!pass && typeof pass=="object") {
            pass="wrongPassword";
        }
    }
    
    if(pass.toLowerCase()!="password"){
        location.replace("http://www.w3.org/");
    }
    /Daniel

  • #8
    Regular Coder
    Join Date
    Jun 2003
    Location
    Spokane, WA
    Posts
    512
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, ya, the thing still WORKS, but it is less secure. If you make a wrong guess or click ok first, and then on the second, third, etc try you push cancel, it lets you in. However, the person should either know the password, or not. So I think I will just stick with the one try thing.

    Thanks again, you were a great help!

    -Ben
    Thoughts determine what you want,
    Actions determine what you get.

  • #9
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP .htaccess works great, but I'm not going there since this is the javascript forum...
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •