1. Home
  2. Computing & Technology
  3. JavaScript

Password Protection

2. Not Coded

clr gif
Join the Discussion

Questions? Comments?

More of this Feature

Introduction Encrypted Password

Having a Javascript test a password where the password to test against is not coded in the Javascript might sound impossible but it can be done by making the password part of the filename of the page that is to be password protected. An example of a Javascript that does this follows:

function pass() {
var password = '';
password=prompt('Please enter your password:','');
if (password != null) {
location.href= password + ".html";
}
}

Of course you should make this an external Javascript to make it slightly more difficult to access the source. All that you need to do then to make a password protected link is to create the page to be password protected and save it in a file with the same name as the password (with .html added to the end) and to create a link to that page (or pages) that looks like this:

<a href="#" onclick="pass();return false;">Access a Password Protected Page</a>

Of course you may still want to use frames to help hide the relationship between the password and the page address.

When the link is selected a prompt box asking to have the password entered is displayed. If the prompt box is closed or cancel is selected then the prompt box will disappear and your visitor will remain on the current page. If a wrong password is entered then the default "page not found" page will be displayed.

Of course using this method doesn't stop someone who has figured out the actual address of the "protected" page from accessing it directly. To protect against that using Javascript we need to encrypt the password into the page itself.

Note that the discussion and scripts on this page are copied from the "Ask Felgall" website with the permission of the copyright owner.

Explore JavaScript
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2010 About.com, a part of The New York Times Company.

All rights reserved.