1. Home
  2. Computing & Technology
  3. JavaScript

Password Protection

3. Encrypted

clr gif
Join the Discussion

Questions? Comments?

More of this Feature

Introduction Not Coded

If you encrypt your web page to try to hide your page source from your visitors then it becomes possible to include a very simple password protection javascript into the page itself that is a lot more effective and which you might even prefer to using a gateway CGI script. This method has the advantage over the other methods in that the password protection no longer acts just as a gate but will ensure that anyone accessing the page needs to enter the password even if they know the exact filename that they want to access. Also, unlike the above script, this one allows you to specify a page to be displayed if a wrong password (or no password) is entered. Also this script can be applied to existing pages without having to recode any of the links to the page.

For this to work, you need to make sure that you encrypt the javascript before including it in the <head> section of the page so that people who know how to access the source of your page without viewing the page first can't easily discover the password. The password is then as secure as your page encryption, the encryption needs to be broken in order for the password to be determined. The rest of the page content also needs to be encrypted or it will be accessible by saving the page source even though the page itself is not viewable.

To create your own custom script simply make substitutions into the following code for your desired password and for a page to display when a wrong password is entered (the two spots not shown in bold text), encrypt the result (including the script tags) and include the resultant encrypted code immediately after the decryptor script.

<script type="text/javascript">var pw = prompt('Password',''); if (pw != 'password') top.location = 'index.html';</script>

Your page will now display a prompt box asking to have the password entered before the actual page content is displayed. If the prompt box is closed, cancel is selected, or a wrong password is entered then the alternate page will be loaded into the browser and the current page will not be displayed.

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

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

All rights reserved.